dongzhan1878 2019-01-02 22: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:

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 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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分