其他分享
首页 > 其他分享> > Laravel with预查询里面有带条件参数时,外面要加whereHas相同条件

Laravel with预查询里面有带条件参数时,外面要加whereHas相同条件

作者:互联网

这种写法不正确,‘notice_cad’ function里面有带条件whereIn,外面要加一层whereHas

           $data = ProofingNotice::with(['notice_cad' => function ($qn) {
                $qn->with(['user_name'])->whereIn('flag', [4]);
            }, 'exploit' => function ($qs) {
                $qs->with(['client', 'brand', 'devedep', 'deveaids', 'client_branch']);
            }, 'notice_cad_review', 'colors'])
                ->whereIn('status', $arr_status)
                ->whereIn('area_id', $place_from)
                ->whereBetween('review_date', [$start_date, $end_date])
                ->get()->toArray();

上面的写法应改为:

$data = ProofingNotice::with(['notice_cad'=>function($w){
                $w->with(['user_name'])->whereIn('flag', [4]);
            }, 'exploit', 'colors','notice_cad_review'])
                ->whereHas('notice_cad', function ($qn) {
                    $qn->with(['user_name'])->whereIn('flag', [4]);
                })
                ->whereHas('exploit', function ($qs) {
                    $qs->with(['client', 'brand', 'devedep', 'deveaids', 'client_branch']);
                })
                ->whereIn('status', $arr_status)
                ->whereIn('area_id', $place_from)
                ->where(function ($qd) use ($start_date, $end_date) {
                    $qd->whereBetween('nuclear_date', [$start_date, $end_date]);
                    $qd->orwhereBetween('transport_date', [$start_date, $end_date]);
                })->get()->toArray();

别问为什么,不知道

标签:Laravel,要加,notice,whereHas,end,function,whereIn,date,cad
来源: https://blog.csdn.net/xcbzsy/article/details/120468227