Search code examples
laravellaravel-5.3laravel-5.4

How to remove quotes from SQL query Laravel?


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

Solution

  • You can try to use DB:raw like this:

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