Laravel - 删除所有关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50503464/
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
Laravel - Delete with all relations
提问by Rainier Laan
I have a system where a user is connected to alot of different tables, These tables are also connected to different tables. When I delete a user from my database, every relation associated with that user should also be deleted. My project's relation setup looks like this
我有一个系统,其中用户连接到许多不同的表,这些表也连接到不同的表。当我从我的数据库中删除一个用户时,与该用户关联的每个关系也应该被删除。我的项目的关系设置看起来像这样
- A User user has multiple orders.
- An Order has multiple order items.
- Order Items belong to an order.
- A webshop belongs to a user and a webshop has One Main Setting.
- Main settings belongs to a webshop and has One address.
- Address belongs to main setting
When a user with the id of 1 gets deleted. All orders where the user_id is equal to 1 should also be deleted, No problems there. But the order with the user_id of 1 also has Many order_items. So say for instance this particular order has the id of 3. All order_items with the order_id of 3 should also be deleted. And that is where I ran into this issue.
当 id 为 1 的用户被删除时。也应该删除 user_id 等于 1 的所有订单,没有问题。但是user_id为1的订单也有很多order_items。例如,假设这个特定订单的 id 为 3。所有 order_id 为 3 的 order_items 也应该被删除。这就是我遇到这个问题的地方。
My attempt
我的尝试
A user gets deleted by doing this
这样做会删除用户
$user = User::find($id);
$user->delete();
A relation gets deleted by doing this:
通过这样做删除关系:
$user->orders()->delete();
But how do I delete the order items associated with all the orders that get deleted? My attempt was:
但是如何删除与所有被删除订单相关的订单项目?我的尝试是:
$user->orders()->orderitems()->delete();
But unfortunately, that does not work. All the relations in the models are working perfectly fine. So all properties that are useable are
但不幸的是,这行不通。模型中的所有关系都运行良好。所以所有可用的属性都是
User
- Orders();
- Webshops();
Order
- Orderitems();
Webshop
- Mainsetting();
Mainsetting
- Address();
How can I accomplish the above?
我怎样才能完成上述任务?
回答by train_fox
If you are using MySQL (or any RDBMS which supports cascading) you can cascade deleting on database level. If don't, you have to handle it manually on controller or by model event listening functions. See documentation on migrationsdetailing foreign key constrains.
如果您使用 MySQL(或任何支持级联的 RDBMS),您可以在数据库级别级联删除。如果没有,您必须在控制器上或通过模型事件侦听功能手动处理它。请参阅详细说明外键约束的迁移文档。
Schema::create('order_items', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('order_id');
$table->timestamps();
$table->foreign('sell_order_id')
->references('id')
->on('orders')
->onDelete('cascade');
});
Note: Laravel supports soft deletesout of the box, as well as cascading soft deletes, see following article https://laravel-news.com/cascading-soft-deletes-eloquent
注:Laravel支持软删除开箱即用的以及级联软删除,请参见下面的文章https://laravel-news.com/cascading-soft-deletes-eloquent
回答by Ronny Fretel
Use foreign keys and delete cascade in your migrations.
在迁移中使用外键和删除级联。
then you only have to delete the orders and automagically the ordesr details will be deleted.
那么您只需删除订单,订单详细信息将自动删除。