DB :: raw不使用模型连接

I'm trying to get a count of model rows from a different database. I have both databases configured fine. My problem is that it doesn't seem to be using the same connection when attempting to perform raw queries.

Code:

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

I am getting error Database [mysql] not configured. Why is this happening and how do I make the raw queries use the model connection?

Got it.

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

Depending on your configuration you may need to do

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