My laravel eloquent is like this :
$products = Product::where('status', 1) ->where('stock', '>', 0) ->where('category_id', '=', $category_id) ->groupBy('store_id') ->orderBy('updated_at', 'desc') ->take(4) ->get();
When executed, there exist error like this :
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'myshop.products.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select * from
products
wherestatus
= 1 andstock
> 0 andcategory_id
= 5 group bystore_id
order byupdated_at
desc limit 4)
How can I solve it?
If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them.
Strict SQL Mode. Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE . A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range.
In MySQL 4.0, a server SQL mode system variable named sql_mode was introduced to allow configuring certain aspects of how the server executes SQL statements. Initially, this variable could be set only by means of the --sql-mode startup option. As MySQL 4.1.
I had a similar Problem and solved it by disabling mysql strict mode in the database connection setting.
'connections' => [ 'mysql' => [ // Behave like MySQL 5.6 'strict' => false, // Behave like MySQL 5.7 'strict' => true, ] ]
You can find even more configuration settings in this blog post by Matt Stauffer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With