我有以下查询:
$this->data = \DB::table('months')->select(DB::raw("months.id, COUNT(transactions.id) as total"))
->leftJoin('transactions', function($join)
{
$join->on('months.id', '=', DB::raw('MONTH(created_at)'))
->on('transactions.doctor_id', '=', $this->user_id);
})
->groupBy('months.id')
->get();
它在线->on('transactions.doctor_id', '=', $this->user_id);
上出现错误。它为变量$this->user_id
添加了单引号。
如何避免这种情绪:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '2' in
'on clause' (SQL: select months.id, COUNT(clients.id) as total
from `months` left join `clients` on `months`.`id` = MONTH(created_at)
and `clients`.`doctor_id` = `2` group by `months`.`id`)
答案 0 :(得分:2)
您可以尝试使用DB:raw
,如下所示:
->on('transactions.doctor_id', '=', DB::raw($this->user_id));