How to remove quotes from SQL query Laravel?

帅比萌擦擦* 提交于 2019-12-08 07:07:16

问题


I have the following query:

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

It involks an error on line ->on('transactions.doctor_id', '=', $this->user_id);. It added single quotes for variable $this->user_id.

How to avoid this eroros:

    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:


You can try to use DB:raw like this:

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


来源:https://stackoverflow.com/questions/46434160/how-to-remove-quotes-from-sql-query-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!