DB :: raw不使用模型连接

时间:2017-06-05 21:58:52

标签: php laravel eloquent

我正在尝试从不同的数据库中获取模型行的数量。我有两个数据库配置正常。我的问题是,在尝试执行原始查询时似乎没有使用相同的连接。

代码:

class Lead extends Model
{
    protected $connection = 'infused2';
    public $timestamps = false;

    function campaignCount(Campaign $campaign)
    {
        $leads = $this->join('lead_history', 'leads.id', '=', 'lead_history.id_lead')->join('assignments', 'leads.id', '=', 'assignments.id_lead');

        if (!empty($campaign->lead_date_created_operand)) {
            if ($campaign->leadDateCreatedOperand->name == 'day age') {
                $leads->where(DB::raw("date_format(from_unixtime(lead_history.date_created), '%d-%m-%Y') = date_format(".strtotime($campaign->lead_date_created_value).", '%d-%m-%Y')"));
            }
            else if ($campaign->leadDateCreatedOperand->name == 'month age') {
                $leads->where(DB::raw("date_format(from_unixtime(lead_history.date_created), '%m-%Y') = date_format(".strtotime($campaign->lead_date_created_value).", '%m-%Y')"));
            }
        }

        if (!empty($campaign->lead_date_assigned_operand)) {
            if ($campaign->leadDateAssignedOperand->name == 'day age') {
                $leads->where(DB::raw("date_format(from_unixtime(assignments.date_assigned), '%d-%m-%Y') = date_format(".strtotime($campaign->lead_date_assigned_value).", '%d-%m-%Y')"));
            }
            else if ($campaign->leadDateAssignedOperand->name == 'month age') {
                $leads->where(DB::raw("date_format(from_unixtime(assignments.date_assigned), '%m-%Y') = date_format(".strtotime($campaign->lead_date_assigned_value).", '%m-%Y')"));
            }
        }

        return $leads->count();
    }
}

我收到错误Database [mysql] not configured。为什么会发生这种情况,如何使原始查询使用模型连接?

2 个答案:

答案 0 :(得分:1)

知道了。

    $query = DB::connection($this->connection)->table($this->getTable());

    if (!empty($campaign->lead_date_created_operand)) {
        $query->join('lead_history', 'lead_history.id_lead', '=', 'leads.id');

        if ($campaign->leadDateCreatedOperand->name == 'day age') {
            $query->whereRaw("date_format(from_unixtime(lead_history.date_created), '%d-%m-%Y') = date_format(from_unixtime(".strtotime('-'.$campaign->lead_date_created_value)."), '%d-%m-%Y')");
        }
        else if ($campaign->leadDateCreatedOperand->name == 'month age') {
            $query->whereRaw("date_format(from_unixtime(lead_history.date_created), '%m-%Y') = date_format(from_unixtime(".strtotime('-'.$campaign->lead_date_created_value)."), '%m-%Y')");
        }
    }

    if (!empty($campaign->lead_date_assigned_operand)) {
        $query->join('assignments', 'assignments.id_lead', '=', 'leads.id');

        if ($campaign->leadDateAssignedOperand->name == 'day age') {
            $query->whereRaw("date_format(from_unixtime(assignments.date_assigned), '%d-%m-%Y') = date_format(from_unixtime(".strtotime('-'.$campaign->lead_date_assigned_value)."), '%d-%m-%Y')");
        }
        else if ($campaign->leadDateAssignedOperand->name == 'month age') {
            $query->whereRaw("date_format(from_unixtime(assignments.date_assigned), '%m-%Y') = date_format(from_unixtime(".strtotime('-'.$campaign->lead_date_assigned_value)."), '%m-%Y')");
        }
    }

    return $query->count();

答案 1 :(得分:0)

根据您的配置,您可能需要执行

DB::connection('infused2')...