首页 > 解决方案 > 我将如何在 laravel eloquent 的急切加载中加入条件

问题描述

    enter code here 
                 $wo=Warehouse_other_receive_detail::with('item_info','item_info.product_sub_group')
                ->where('item_info.product_sub_group->sub_group_id', 2)
                ->get();


                 $wo = Warehouse_other_receive_detail::with(['item_info'=>function( $q){
                $q->getQuery()->has('product_sub_group')->where('sub_group_id', 2);
                     }])
                ->get();

//但是第二个代码不是内部连接我想第一个解决//我将如何访问主模型中的函数模型特定列----解决这个->>

标签: laraveleloquenteager-loading

解决方案


使用 whereHas 而不是 where。您可以使用 whereHas 方法完成您想要的操作,该方法将对关系执行查询:

$wo = Warehouse_other_receive_detail::with('item_info')->whereHas('item_info', function ($query){
    $query->where('sub_group_id',2);
})->get();

推荐阅读