Laravel 删除belongsToMany 关系中的所有belongsToMany 关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47659484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:03:11  来源:igfitidea点击:

Laravel delete all belongsToMany relations in belongsToMany relation

laravellaravel-5eloquent

提问by Joas

I have a Tournament model which contains a belongsToMany relationship with a Session model:

我有一个 Tournament 模型,它包含一个与 Session 模型的belongsToMany 关系:

class Tournament extends Model{

    public function sessions(){
        return $this->belongsToMany(Session::class, 'tournament_has_sessions');
    }
}

Then my sessions model contains a belongsToMany relationship with the User model:

然后我的会话模型包含与用户模型的belongsToMany 关系:

class Session extends Model{

    public function users(){
        return $this->belongsToMany(User::class, 'session_has_users');
    }

}

Now when I delete a Tournament, I want to delete all sessions and all information in the session_has_users table with it.

现在,当我删除锦标赛时,我想用它删除 session_has_users 表中的所有会话和所有信息。

I have tried:

我试过了:

$tournament->sessions()->with('users')->delete(); -- < This
$tournament->sessions()->delete();
$tournament->users()->detach();
$tournament->delete();

But it does't work. The data in the session_has_users table persists

但它不起作用。session_has_users 表中的数据持久化

I realize i could do something like this:

我意识到我可以做这样的事情:

DB::table('session_has_users')->whereIn('session_id', $this->sessions()->pluck('id'))->delete();

But is there a more efficient/handy (or even alternative if not more efficient) way to accomplish this?

但是有没有更有效/更方便(或者即使不是更有效的替代方法)来实现这一目标?

回答by Devon

Using RDBMS Referential Actions on the Foreign Key

对外键使用 RDBMS 引用操作

In the database schema, you should define the foreign key with the ON DELETE CASCADEaction. This will automatically delete the related records when the referenced id is deleted.

在数据库模式中,您应该使用操作定义外键ON DELETE CASCADE。这将在删除引用的 id 时自动删除相关记录。

(See dbudimir's answer for the example lines from a Laravel migration file)

(有关 Laravel 迁移文件中的示例行,请参阅 dbudimir 的回答)



Using Eloquent to Detach All Related Models

使用 Eloquent 分离所有相关模型

If you aren't using foreign keys or your database lacks the support for referential actions, you can use this:

如果您不使用外键或您的数据库缺乏对引用操作的支持,您可以使用:

$tourament->sessions()->sync([]);

The sync method accepts an array of IDs to place on the intermediate table. Any IDs that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the IDs in the given array will exist in the intermediate table:

同步方法接受要放置在中间表上的一组 ID。任何不在给定数组中的 ID 都将从中间表中删除。因此,在此操作完成后,中间表中将只存在给定数组中的 ID:

https://laravel.com/docs/5.5/eloquent-relationships#updating-many-to-many-relationships

https://laravel.com/docs/5.5/eloquent-relationships#updating-many-to-many-relationships

Providing an empty array should then remove all the relations.

提供一个空数组应该删除所有关系。

回答by dbudimir

In the table 'tournament_has_sessions' you can set onDelete('cascade') like this

在表 'tournament_has_sessions' 中,您可以像这样设置 onDelete('cascade')

$table->integer('tournament_id')->unsigned()->nullable();
$table->foreign('tournament_id')->references('id')->on('tournaments')->onDelete('cascade');    
$table->integer('session_id')->unsigned()->nullable();
$table->foreign('session_id')->references('id')->on('sessions')->onDelete('cascade');

And when delete tournaments automaticly deleted all records in this table

并且删除比赛时自动删除了该表中的所有记录

回答by TheGeeky

you can use any of these:

您可以使用以下任何一种:

$tourament->sessions()->detach();

$tourament->sessions()->detach();

or

或者

$tourament->sessions()->sync([]);

$tourament->sessions()->sync([]);

or you can define the foreign key with the ON DELETE CASCADE like the answers above

或者您可以像上面的答案一样使用 ON DELETE CASCADE 定义外键