GROUP BY
The club GROUP BY
accepts two types of information: text and whole. When an integer value is informed, you will be informing - implicitly - the position of the column that will be used in the above clause.
Example:
SELECT `id`, `name`, `gender` FROM `users` GROUP BY 3
Amounts to the code:
SELECT `id`, `name`, `gender` FROM `users` GROUP BY `gender`
Erro no Laravel
By default, all information passed in the methods: table
, select
, groupBy
, orderBy
etc. They are placed between severe accents. This will ensure that when using column names such as date
and order
(which are native functions of the SQL), the code does not fail, as names between severe accents will not be interpreted by Mysql, for example.
Example in Laravel:
$test = \DB::table('users')->select('id')->groupBy('1');
dd($test->toSQL());
"select `id` from `users` group by `1`"
Note that in the above output, the method groupBy
automatically puts the numeric value between low accents. Causing the Mysql interpret it as a column and not her position.
To fix this fault, you can use the method DB::raw
, for example:
$test = \DB::table('users')->select('id')->groupBy(\DB::raw('1'));
dd($test->toSQL());
"select id
from users
group by 1"
With the DB::raw
, you will inform Laravel that it is not to change or escape that code.
Strict Mode
According to the documentation, the Strict mode controls how Mysql handles invalid or missing values in data change instructions.
Although it does not generate an error, a warning is generated when using the select
with the strict mode set as false
.
With the strict mode active, the Mysql will generate errors when trying to use invalid values (as in the example of group by
). With the above idle mode, the Database Manager System (SGDB) will try to adjust the value so that such errors are not displayed, however the result of the query may be affected.
Sincerity, this is not a flaw of
Eloquent
(Laravel é o framework Web
) andEloquent
which is used within the Laravel and can also be used outside, ie it is independent of theLaravel
even if it’s done by the same guy. 'Cause that’s how it is? because it is not all banks that work in the same way and the standard placed by it is used in all banks. It is worth remembering that there is in all its methods the way to circumventSQL Raw
and that you explained. Don’t get me wrong, but I need to challenge your answer on this topic because Error is what doesn’t work.– novic