dongzhan1878 2019-01-02 14:33
浏览 134
已采纳

Laravel 5.7 LeftJoin从两个表中删除行

I have been looking around for ways to delete some rows from joined tables in Laravel 5.7. I have three tables(Inventories, Vehicles and Vimages) and I want to join them all together and delete the rows together. Does anyone know if this is possible?

The reason I am trying to do this is because the Vehicles and Inventories table have a one to one relationship, which only the Inventories table has an id which is used to sort the vehicles(Dealer Id). So I can select multiple vehicles from Inventories with the dealer id and delete them but I cannot do the same with the Vehicles table.

Thanks

My Join:

  1. Inventory::leftJoin(
  2. 'vehicles', 'vehicles.id', '=', 'inventories.vehicle_id'
  3. )->leftJoin(
  4. 'vimages', 'vimages.inventory_id', '=', 'inventories.id'
  5. )->where(
  6. 'inventories.dealer_id', '=', \Auth::user()->dealer_id
  • 写回答

1条回答 默认 最新

  • dongzhiqi0332 2019-01-02 15:20
    关注

    If you use a foreign key constraint with cascading deletes you can achieve this. Eloquent supports this but you need to set this up in your migrations as follows.

    $table->foreign('vehicle_id')->references('id')->on('vehicles')->onDelete('cascade');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部