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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作