【PHP】多対多
参考ページ
構成と概要
- CategoryとQuestionテーブルのリレーション
- Code
- belongsToMany('リレーション先Modelのパス')
- unsignedBigInteger
- $table->foreign('カラム名')->references('id')->on('テーブル名')->onDelete('cascade');
Questionsテーブル
- model
class Question extends Model
{
use HasFactory;
protected $fillable = [
'body',
'choice_1',
'choice_2',
'choice_3',
'choice_4',
'answer_body',
'answer_choice',
'status_num',
'user_id',
];
public function workbooks()
{
return $this->belongsToMany('App\Workbook');
}
public function categories()
{
return $this->belongsToMany('App\Category');
}
public function users()
{
return $this->belongsTO('App\User');
}
}
- Migration File
class CreateQuestionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('questions', function (Blueprint $table) {
$table->id();
$table->string('body',500);
$table->string('choice_1',100);
$table->string('choice_2',100);
$table->string('choice_3',100);
$table->string('choice_4',100);
$table->string('answer_body');
$table->integer('answer_choice');
$table->integer('status_num');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('questions');
}
}
Categoriesテーブル
- model
class Category extends Model
{
use HasFactory;
protected $fillable = [
'body',
];
public function questions()
{
return $this->belongsToMany('App\Question');
}
}
- Migration File
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('body',30);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
中間テーブル
- Model
class Category_question extends Model
{
use HasFactory;
protected $fillable = [
'workbook_id',
'question_id',
];
}
- Migration File
public function up()
{
Schema::create('category_questions', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('question_id');
$table->timestamps();
$table->foreign('category_id')
->references('id')
->on('categories')
->onDelete('cascade');
$table->foreign('question_id')
->references('id')
->on('questions')
->onDelete('cascade');
});
}
- belongsToMany('リレーション先Modelのパス')
- unsignedBigInteger
- $table->foreign('カラム名')->references('id')->on('テーブル名')->onDelete('cascade');
- model
class Question extends Model
{
use HasFactory;
protected $fillable = [
'body',
'choice_1',
'choice_2',
'choice_3',
'choice_4',
'answer_body',
'answer_choice',
'status_num',
'user_id',
];
public function workbooks()
{
return $this->belongsToMany('App\Workbook');
}
public function categories()
{
return $this->belongsToMany('App\Category');
}
public function users()
{
return $this->belongsTO('App\User');
}
}
- Migration File
class CreateQuestionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('questions', function (Blueprint $table) {
$table->id();
$table->string('body',500);
$table->string('choice_1',100);
$table->string('choice_2',100);
$table->string('choice_3',100);
$table->string('choice_4',100);
$table->string('answer_body');
$table->integer('answer_choice');
$table->integer('status_num');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('questions');
}
}
Categoriesテーブル
- model
class Category extends Model
{
use HasFactory;
protected $fillable = [
'body',
];
public function questions()
{
return $this->belongsToMany('App\Question');
}
}
- Migration File
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('body',30);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
中間テーブル
- Model
class Category_question extends Model
{
use HasFactory;
protected $fillable = [
'workbook_id',
'question_id',
];
}
- Migration File
public function up()
{
Schema::create('category_questions', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('question_id');
$table->timestamps();
$table->foreign('category_id')
->references('id')
->on('categories')
->onDelete('cascade');
$table->foreign('question_id')
->references('id')
->on('questions')
->onDelete('cascade');
});
}
class Category extends Model
{
use HasFactory;
protected $fillable = [
'body',
];
public function questions()
{
return $this->belongsToMany('App\Question');
}
}
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('body',30);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
- Model
class Category_question extends Model
{
use HasFactory;
protected $fillable = [
'workbook_id',
'question_id',
];
}
- Migration File
public function up()
{
Schema::create('category_questions', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('question_id');
$table->timestamps();
$table->foreign('category_id')
->references('id')
->on('categories')
->onDelete('cascade');
$table->foreign('question_id')
->references('id')
->on('questions')
->onDelete('cascade');
});
}
Author And Source
この問題について(【PHP】多対多), 我々は、より多くの情報をここで見つけました https://qiita.com/RealXiaoLin/items/e296cb391e575d290665著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .