dongzhan1878
dongzhan1878
2019-01-02 22:33
浏览 104
已采纳

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:

Inventory::leftJoin(
                'vehicles', 'vehicles.id', '=', 'inventories.vehicle_id'
            )->leftJoin(
                'vimages', 'vimages.inventory_id', '=', 'inventories.id'
            )->where(
                'inventories.dealer_id', '=', \Auth::user()->dealer_id
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhiqi0332
    dongzhiqi0332 2019-01-02 23: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');
    
    点赞 评论

相关推荐