【问题标题】:Laravel Eloquent: Multiple Wheres with OrWhere [duplicate]Laravel Eloquent:OrWhere 的多个 Wheres [重复]
【发布时间】:2018-10-10 23:58:11
【问题描述】:

我的应用程序中有一个搜索功能,可以搜索多个列的“广泛”搜索词或用户选择的定义列。我还有一个外部 where 语句正在搜索公司 ID(多租户应用程序)和分配的 subcompany_id,两者都属于用户。

当我搜索时,我得到了所有结果,并且没有使用与用户相关的 company_id 或 subcompany_id。我发现它在or where 语句括号内使用了 subcompany_id,而不是在括号外。

版本:Laravel 5.6

$request->broad = 广泛搜索的复选框。

查询:

$Leads = Lead::with('Bank')
                ->with('BankBranch')
                ->with('Account')
                ->with('LeadStatus')
                ->with('SalesRep')
                ->when($HideAccounts == True, function ($HideAccounts) {
                    $HideAccounts->where('lead_merchant_id', '');
                })
                ->when(isset($request), function ($CustomSearch) use ($request) {
                    $CustomSearch->when(!empty($request->broad), function ($BroadSearch) use ($request) {
                        $BroadSearch->where('lead_name', 'LIKE', '%' . $request->broad . '%')
                            ->orWhere('lead_contact', 'LIKE', '%' . $request->broad . '%')
                            ->orWhere('lead_phone1', 'LIKE', '%' . $request->broad . '%')
                            ->orWhere('lead_phone2', 'LIKE', '%' . $request->broad . '%')
                            ->orWhere('lead_merchant_id', $request->broad)
                            ->orWhere('secondary_mid', $request->broad)
                            ->orWhere('lead_address_city', $request->broad)
                            ->orWhere('lead_address_state', $request->broad)
                            ->orWhere('lead_address_zip', 'LIKE', '%' . $request->broad . '%');
                    });

                    if(!empty($request->company_name)) {
                        $CustomSearch->where('lead_name', 'LIKE', '%' . $request->company_name . '%');
                    }

                    if(!empty($request->lead_contact)) {
                        $CustomSearch->where('lead_contact', 'LIKE', '%' . $request->lead_contact . '%');
                    }

                    if(!empty($request->address_city)) {
                        $CustomSearch->where('lead_address_city', $request->address_city);
                    }

                    if(!empty($request->address_state)) {
                        $CustomSearch->where('lead_address_state', $request->address_state);
                    }

                    if (!empty($request->sic_code)) {
                        $CustomSearch->where('lead_sic_code', 'LIKE', '%' . $request->sic_code . '%');
                    }

                    if (!empty($request->lead_leadstatus_id)) {
                        $CustomSearch->where('lead_leadstatus_id', $request->lead_leadstatus_id);
                    }

                    if(!empty($request->address_zip)) {
                        $CustomSearch->where('lead_address_zip', 'LIKE', '%' . $request->address_zip . '%');
                    }

                    if(!empty($request->phone)) {
                        $CustomSearch->where('lead_phone1', $request->phone);
                        $CustomSearch->orWhere('lead_phone2', $request->phone);
                    }

                    if (!empty($request->lead_referral_user_id)) {
                        $CustomSearch->where('lead_referral_user_id', $request->lead_referral_user_id);
                    }

                    if (!empty($request->lead_sales_representative_id)) {
                        $CustomSearch->where('lead_sales_representative_id', $request->lead_sales_representative_id);
                    }

                    if (!empty($request->lead_referral_bank_id)) {
                        $CustomSearch->where('lead_referral_bank_id', $request->lead_referral_bank_id);
                    }

                    if (!empty($request->lead_referral_bankbranch_id)) {
                        $CustomSearch->where('lead_referral_bankbranch_id', $request->lead_referral_bankbranch_id);
                    }

                    if (!empty($request->lead_created)) {
                        $LeadCreated = Carbon::createFromFormat('M d, Y', $request->lead_created)->startOfDay();

                        if (!empty($LeadCreated)) {
                            $CustomSearch->where('lead_created_timestamp', '>=', $LeadCreated);
                        }
                    }

                    if (!empty($request->lead_created_end)) {
                        try {
                            $LeadCreatedEnd = Carbon::createFromFormat('M d, Y', $request->lead_created_end)->startOfDay();
                        } catch (\Exception $e) {
                            $LeadCreatedEnd = NULL;
                        }

                        if (!empty($LeadCreatedEnd)) {
                            $CustomSearch->where('lead_created_timestamp', '<=', $LeadCreatedEnd);
                        }
                    }

                    if (!empty($request->account_approval_start)) {
                        try {
                            $AccountApprovalStart = Carbon::createFromFormat('M d, Y', $request->account_approval_start)->startOfDay();
                        } catch (\Exception $e) {
                            $AccountApprovalStart = NULL;
                        }

                        if (!empty($AccountApprovalStart)) {
                            $CustomSearch->whereHas('Account', function ($Account) use ($AccountApprovalStart) {
                                $Account->where('account_created_timestamp', '>=', $AccountApprovalStart);
                            });
                        }
                    }

                    if (!empty($request->account_approval_end)) {
                        try {
                            $AccountApprovalEnd = Carbon::createFromFormat('M d, Y', $request->account_approval_end)->startOfDay();
                        } catch (\Exception $e) {
                            $AccountApprovalEnd = NULL;
                        }

                        if (!empty($AccountApprovalEnd)) {
                            $CustomSearch->whereHas('Account', function ($Account) use ($AccountApprovalEnd) {
                                $Account->where('account_created_timestamp', '<=', $AccountApprovalEnd);
                            });
                        }
                    }
                })
                ->where('lead_company_id', Auth::user()->user_company_id)
                ->when(Auth::user()->user_subcompany_id != NULL, function ($query) {
                    return $query->where('lead_subcompany_id', Auth::user()->user_subcompany_id);
                });

此代码返回以下查询:

select count(*) as aggregate from `leads` where (`lead_name` LIKE '%tire%' or `lead_contact` LIKE '%tire%' or `lead_phone1` LIKE '%tire%' or `lead_phone2` LIKE '%tire%' or `lead_merchant_id` = 'tire' or `secondary_mid` = 'tire' or `lead_address_city` = 'tire' or `lead_address_state` = 'tire' or `lead_address_zip` LIKE '%tire%' and `lead_deleted` = '0' and `lead_duplicate` &lt;&gt; '1' and `lead_company_id` = '1' and `lead_subcompany_id` = '1') and `leads`.`lead_deleted_timestamp` is null

它应该做什么:

select count(*) as aggregate from `leads` where (`lead_name` LIKE '%tire%' or `lead_contact` LIKE '%tire%' or `lead_phone1` LIKE '%tire%' or `lead_phone2` LIKE '%tire%' or `lead_merchant_id` = 'tire' or `secondary_mid` = 'tire' or `lead_address_city` = 'tire' or `lead_address_state` = 'tire' or `lead_address_zip` LIKE '%tire%' and `lead_deleted` = '0' and `lead_duplicate` &lt;&gt; '1') and `lead_company_id` = '1' and `lead_subcompany_id` = '1' and `leads`.`lead_deleted_timestamp` is null`

如何使用 Laravel Eloquent 实现这一点?

【问题讨论】:

    标签: laravel laravel-5 eloquent


    【解决方案1】:

    如果你想在“$BroadSearch”部分加上括号,那么你需要把查询放在它自己的范围内:

    $BroadSearch->where(function($query) {
        $query->where('lead_name', 'LIKE', '%' . $request->broad . '%')
            ->orWhere('lead_contact', 'LIKE', '%' . $request->broad . '%')
            ->orWhere('lead_phone1', 'LIKE', '%' . $request->broad . '%')
            ->orWhere('lead_phone2', 'LIKE', '%' . $request->broad . '%')
            ->orWhere('lead_merchant_id', $request->broad)
            ->orWhere('secondary_mid', $request->broad)
            ->orWhere('lead_address_city', $request->broad)
            ->orWhere('lead_address_state', $request->broad)
            ->orWhere('lead_address_zip', 'LIKE', '%' . $request->broad . '%');
    });
    

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 2011-04-19
      • 2014-02-14
      • 2020-06-16
      • 2017-11-07
      • 2022-01-04
      • 2018-03-22
      • 1970-01-01
      • 2015-06-15
      相关资源
      最近更新 更多