【发布时间】:2017-12-27 16:34:07
【问题描述】:
您将如何在下表之间创建关系:
用户表:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('firstname');
$table->string('surename');
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('country')->nullable();
$table->string('postcode')->nullable();
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
设置类型表:
Schema::create('setting_types', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
设置表:
Schema::create('settings', function (Blueprint $table) {
$table->increments('id');
$table->string('value');
$table->timestamps();
});
setting_type_user 表:
Schema::create('setting_type_user', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('type_id')->unsigned();
$table->integer('setting_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('type_id')->references('id')->on('setting_types');
$table->foreign('setting_id')->references('id')->on('settings');
$table->timestamps();
});
这是我想要得到的结果:
{"id":1,"value":"578943205.jpg","created_at":"2017-07-18 00:00:00","updated_at":null,"pivot":{"setting_id":1,"user_id":1,"type_id":1}}
【问题讨论】:
标签: laravel eloquent relationship