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.
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?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire