如何从SQL查询Laravel中删除引号?

时间:2017-09-26 19:14:14

标签: laravel laravel-5.3 laravel-5.4

我有以下查询:

$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`)

1 个答案:

答案 0 :(得分:2)

您可以尝试使用DB:raw,如下所示:

->on('transactions.doctor_id', '=', DB::raw($this->user_id));