Php DB::raw未使用模型连接

Php DB::raw未使用模型连接,php,laravel,eloquent,Php,Laravel,Eloquent,我试图从另一个数据库中获取模型行数。我两个数据库都配置得很好。我的问题是,在尝试执行原始查询时,它似乎没有使用相同的连接 代码: 我收到错误数据库[mysql]未配置。为什么会发生这种情况?如何使原始查询使用模型连接?取决于您可能需要执行的配置 DB::connection('infused2')... 明白了 $query = DB::connection($this->connection)->table($this->getTable()); if (

我试图从另一个数据库中获取模型行数。我两个数据库都配置得很好。我的问题是,在尝试执行原始查询时,它似乎没有使用相同的连接

代码:


我收到错误
数据库[mysql]未配置
。为什么会发生这种情况?如何使原始查询使用模型连接?

取决于您可能需要执行的配置

DB::connection('infused2')...
明白了

    $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();

在你的app/config/database.php上,你的connections下面有“infused2”吗?您可能会使用infused1和infused2。是的,我在数据库配置中设置了它。活动也是一个模型吗?如果是,它是否驻留在另一个数据库中?如果答案是肯定的,您是否也有活动的受保护$connection='db_name'?活动也是一个模型,并且活动指定了其连接参数。请转到控制台并键入以下内容(不带引号):“php artisan tinker”。然后键入:“test=DB::connection('infused1')->getPdo();”您看到正常运行时间了吗?如果答案是肯定的,那么对D2也要这样做。它是否也显示正常运行时间?
    $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();