Jc John
Jc John

Reputation: 1859

laravel 5 inner join and group by query syntax error

please help me with my format query in laravel 5. I am a beginner in using laravel 5, and i just search to came up with this query, but now i am getting error i dont know where and why? i think my error is only in syntax?

Here is my code:

$date = date("Y-m-d", strtotime($request->datepicker2));

$products =  DB::table('shipping_table')
->select('products.product_name', 'products.price', 'Sum(shipping_products.quantity) AS qtysold', 'shipping_table.sold_date')
->join('shipping_products','shipping_table.shipping_id', '=', 'shipping_products.shipping_id')
->join('products','products.product_id', '=', 'shipping_products.product_id')
->where([['DATE(shipping_table.sold_date)', '=',$date], ['shipping_table.shipping_status', '=' ,1]])
->groupBy('products.product_name')
->paginate(8);

any suggestions? i printed the error and it says this :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'DATE(shipping_table.sold_date)' in 'where clause'

Upvotes: 1

Views: 3612

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72289

You have extra starting [ in this line:-

->where([['DATE(shipping_table.sold_date)', '=',$date, ['shipping_table.shipping_status', '=' ,1]])

It need to be (use DB::raw() to use mysql functions):-

$date = date("Y-m-d", strtotime($request->datepicker2));

$products =  DB::table('shipping_table')
->select('products.product_name', 'products.price', DB::raw("Sum(shipping_products.quantity) as qtysold"), 'shipping_table.sold_date')
->join('shipping_products','shipping_table.shipping_id', '=', 'shipping_products.shipping_id')
->join('products','products.product_id', '=', 'shipping_products.product_id')
->where([[DB::raw("date(shipping_table.sold_date)"),$date], ['shipping_table.shipping_status', '=' ,1]])
->groupBy('products.product_name')
->paginate(8);

Note:-

Regarding the error you get (Syntax error or access violation: 1055) check this answer and do accordingly:-

aravel : Syntax error or access violation: 1055 Error

Upvotes: 3

Related Questions