如何从 SQL 查询 Laravel 中删除引号?
How to remove quotes from SQL query Laravel?
我有以下查询:
$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`)
您可以尝试这样使用 DB:raw
:
->on('transactions.doctor_id', '=', DB::raw($this->user_id));
我有以下查询:
$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`)
您可以尝试这样使用 DB:raw
:
->on('transactions.doctor_id', '=', DB::raw($this->user_id));