justinus andreas
justinus andreas

Reputation: 31

Laravel error when get data from mySQL using groupBy

I want to get Data from mySQL where its only get 1 id_product,so when there is 2 data with same id_product value.. its only get the first one.I have tried the query in mySQL and its work, the query is like this

select `product_photo`.*, `product`.`id_merchant` from `product_photo` inner join `product` on 
`product_photo`.`id_product` = `product`.`id` where `product`.`id_merchant` = 11 group by 
`product_photo`.`id_product` 

but i got error when i implemented it to my laravel controller,the code is like this

$id_merchant = $request->input('id_merchant');

    $photo=DB::table('product_photo')
    ->join('product','product_photo.id_product','=','product.id')
    ->select('product_photo.*','product.id_merchant')
    ->where('product.id_merchant','=',$id_merchant)
    ->groupBy('product_photo.id_product')
    ->get();
    
    return response()->json(['success'=>true,'message'=>'success', 'data' => $photo],200);

Can someone tell me why my laravel got error ? Sorry my english

Upvotes: 0

Views: 114

Answers (1)

Silidrone
Silidrone

Reputation: 1571

This is caused by MySQL's strict mode. Change strict to false in your config/database.php file.

When you open the file, in the mysql array, set strict => false. After you disable MySQL's strict mode it shouldn't show the error anymore.

Upvotes: 2

Related Questions