Laravel 5.2或者在查询中生成“和”

时间:2017-07-11 09:55:39

标签: laravel laravel-5

我在Laravel 5.2编写了一个查询,如下所示,我使用了范围。范围scopeSubscriberCriteriaSearch具有进一步的子范围。方案是,如果主范围找到"first_name" "=" "test" OR "last_name" "!=" "demo"等,则将调用相关的子范围。

    $record = self::leftJoin("$customersTable as customers", 'customers.email', '=', "{$this->table}.email")
        ->leftJoin("$campaignsTable as campaigns", 'campaigns.campaign_id', '=', "{$this->table}.campaign_id")
        ->select("$this->primaryKey", "{$this->table}.first_name", "{$this->table}.last_name", "{$this->table}.email", "{$this->table}.phone", "{$this->table}.address", "campaigns.campaign_id", "campaigns.campaign_name", "customers.order_count")
        ->whereRaw($where);
$record->subscriberCriteriaSearch($criteria, $search_type, $persist)
        ->searchAfter($waiting_min, $search_type, $this->user_id)
        ->excludeLeadEmail($last_used_email)
        ->greaterCreateDateSearch($campaign_start_date)
        ->get();

=========== 主要范围如下:

public function scopeSubscriberCriteriaSearch($query, $criteria, $search_type, $persist = TRUE)
{   
    if (!empty($criteria))
    {
        $criteria_var = ['first_name', 'last_name', 'email', 'phone_number', 'city', 'country', 'state', 'zip', 'address', 'campaign', 'affiliate', 'subAffiliate', 'createdAt'];
        return $query->where(function($sub_query) use ($criteria, $criteria_var, $search_type, $persist)
            {
                foreach ($criteria as $rule)
                {
                    if (in_array($rule->condition, $criteria_var))
                    {
                        $position = array_search($rule->condition, $criteria_var);
                        ($rule->condition == 'affiliate' || $rule->condition == 'subAffiliate') ? $sub_query->{$criteria_var[$position] . 'Search'}($rule->logical, $rule->input_val, $search_type, $persist) : $sub_query->{$criteria_var[$position] . 'Search'}($rule->logical, $rule->input_val, $persist);
                    }
                }
            });
    }
}

子范围(例如:)如下:

public function scopeFirst_nameSearch($query, $operator, $first_name, $persist = TRUE)
{
    $query_operator = $this->getQueryOperator($operator);
    if (!empty($operator) && !empty($first_name))
    {
        $query_operator_val = preg_match('/like/i', $query_operator) ? ($operator == 'starts' ? '"' . $first_name . '%"' : ($operator == 'ends' ? '"%' . $first_name . '"' : '"%' . $first_name . '%"')) : '"' . $first_name . '"';

        return $persist ? $query->where($this->table . ".first_name", $query_operator, DB::raw($query_operator_val)) : $query->orWhere($this->table . ".first_name", $query_operator, DB::raw($query_operator_val));
    }
}

public function scopeLast_nameSearch($query, $operator, $last_name, $persist = TRUE)
{
    $query_operator = $this->getQueryOperator($operator);
    if (!empty($operator) && !empty($last_name))
    {
        $query_operator_val = preg_match('/like/i', $query_operator) ? ($operator == 'starts' ? '"' . $last_name . '%"' : ($operator == 'ends' ? '"%' . $last_name . '"' : '"%' . $last_name . '%"')) : '"' . $last_name . '"';
        return $persist ? $query->where($this->table . ".last_name", $query_operator, DB::raw($query_operator_val)) : $query->orWhere($this->table . ".last_name", $query_operator, DB::raw($query_operator_val));
    }
}

因此,如果$ persist为False,则这些子查询的orWhere部分将执行。但在我的情况下,查询现在生成如下:

选择prospect_idsem_1_prospectsfirst_namesem_1_prospectslast_namesem_1_prospectsemail,{{1} }。sem_1_prospectsphonesem_1_prospectsaddresssem_campaignscampaign_idsem_campaignscampaign_name。来自sem_customers order_count的{​​{1}} sem_1_prospects sem_1_customers sem_customers sem_customers emailsem_1_prospects左边加入email上的sem_1_campaignssem_campaignssem_campaigns = campaign_idsem_1_prospects其中((campaign_idsem_1_prospects = “test”)和(first_namesem_1_prospects不喜欢“%demo%”));

有人可以帮我找出问题吗?

0 个答案:

没有答案