Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I solve incompatible with sql_mode=only_full_group_by in laravel eloquent?

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 where status = 1 and stock > 0 and category_id = 5 group by store_id order by updated_at desc limit 4)

How can I solve it?

like image 842
samuel toh Avatar asked May 04 '17 07:05

samuel toh


People also ask

What is Sql_mode Only_full_group_by?

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.

What is strict SQL mode?

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.

What is Sql_mode?

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.


1 Answers

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

like image 79
naabster Avatar answered Oct 08 '22 03:10

naabster