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

38 篇文章 1 订阅
35 篇文章 2 订阅

这种写法不正确,‘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();

别问为什么,不知道

一、
            $data = ProofingNotice::with(['notice_cad'=>function($w){
                $w->with(['user_name'])->whereIn('flag', [12])->where(['state'=>0]);
            }, 'exploit', 'colors','notice_cad_review'])
            ->whereHas('notice_cad', function ($qn) {
                $qn->with(['user_name'])->whereIn('flag', [12])->where(['state'=>0]);
            })
            ->whereHas('exploit', function ($qs) {
                $qs->with(['client', 'brand', 'devedep', 'deveaids', 'client_branch']);
            })
            ->whereIn('status', $arr_status)
            ->whereIn('area_id', $place_from)
            ->where('properties_id','<>',15) //15局部样
            ->where(function ($qd) use ($start_date, $end_date) {
                $qd->whereBetween('review_date', [$start_date, $end_date]);
            })->get()->toArray();

二、
           $data = ProofingNotice::with(['notice_size',
                    'exploit'=>function($qe) use($depid,$client_id,$brand_id,$deve_aid){
                        if (isset($depid)){
                            $qe->with(['client','brand','devedep','deveaids','client_branch'])
                               ->whereIn('deve_dep',$depid);
                        }
                        if (isset($client_id)){
                            $qe->with(['client','brand','devedep','deveaids','client_branch'])
                                ->where('client_id',$client_id);
                        }
                        if (isset($brand_id)){
                            $qe->with(['client','brand','devedep','deveaids','client_branch'])
                                ->where('brand_id',$brand_id);
                        }
                        if (isset($deve_aid)){
                            $qe->with(['client','brand','devedep','deveaids','client_branch'])
                                ->where('deve_aid',$deve_aid);
                        }
                        if( !isset($depid) && !isset($client_id) && !isset($brand_id)  && !isset($deve_aid) ) {
                            $qe->with(['client','brand','devedep','deveaids','client_branch']);
                        }
                    },
                    'colors'=>function($qc) use($place_id){
                        if (isset($place_id)) {
                            $qc->where(['place_id' => $place_id]);
                        }
                    }
                ])
                ->whereHas('exploit', function ($qe) use($depid,$client_id,$brand_id,$deve_aid) {
                    if (isset($depid)){
                        $qe->with(['client','brand','devedep','deveaids','client_branch'])
                           ->whereIn('deve_dep',$depid);
                    }
                    if (isset($client_id)){
                        $qe->with(['client','brand','devedep','deveaids','client_branch'])
                            ->where('client_id',$client_id);
                    }
                    if (isset($brand_id)){
                        $qe->with(['client','brand','devedep','deveaids','client_branch'])
                            ->where('brand_id',$brand_id);
                    }
                    if (isset($deve_aid)){
                        $qe->with(['client','brand','devedep','deveaids','client_branch'])
                            ->where('deve_aid',$deve_aid);
                    }
                    if( !isset($depid) && !isset($client_id) && !isset($brand_id)  && !isset($deve_aid)) {
                        $qe->with(['client','brand','devedep','deveaids','client_branch']);
                    }
                })
                ->whereHas('colors',function ($qc) use($place_id){
                    if (isset($place_id)) {
                        $qc->where(['place_id' => $place_id]);
                    }
                })
                ->whereIn('status',$arr_status)
                ->whereIn('area_id',$place_from)
                ->whereIn('properties_id',$arr_properties)
                ->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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值