当我从组表中删除一行时,我想要该函数也从表group_user中删除所有关系映射。例如,如果我删除了id为4的组,group_user表中group_user表中的所有行也会被删除。如下表:Laravel 5.4从相关表中删除表行和行
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('phone');
$table->boolean('approved')->default(false);
$table->rememberToken();
$table->timestamps();
});
Schema::create('groups', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('course_id');
$table->timestamp('start_date')->nullable();
$table->timestamp('end_date')->nullable();
$table->timestamps();
});
Schema::create('group_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('group_id');
$table->timestamps();
});
目前使用组控制这个功能,但它只能从组表中删除:
function deleteGroup($id){
\t \t $group = Group::find($id);
\t \t $group->delete();
\t \t return redirect()->back();
\t }
我想你想的一些定义,如:'$表 - >外国( 'USER_ID') - 上( 'gropu')> - - >引用( '身份证')> onDelete( '串联'); ' – mkaatman
@mkaatman抱歉,我没有看到您的评论;)我认为您的评论是正确的解决方案。 –