2

I was trying to use groupBy for grouping all views by uni_id and their date of creation so I can generate reports. But I see Laravel Eloquent is generating error. However when I executed the same query in phpmyadmin, it shows me data. How does Eloquent process its queries?

I am using Laravel 5.2

This is the query I am trying to run through Eloquent:

    $data = UniversityView::
    groupBy('created_at', 'uni_id')
    ->select('uni_id', DB::raw('count(*) as views'), 'name', 'created_at')
    ->get();

The question is not how to fix it. I fixed it already by putting 'name' in groupBy. But the question is why query is executing differently through Eloquent?

Check images below for help. Thanks.

enter image description here enter image description here

1 Answer 1

2

That's caused by MySQL's strict mode, it is specified in your connection parameters array at config/database.php that's why when you execute it directly at phpMyAdmin it is working, because phpMyAdmin uses different connection parameters.

You can disable it partially, to allow this kind of groupsBy, by modifying your config/database.php connection array. Do it in this way:

Add modes array to only disable this rule:

 'strict' => true,
 'modes' => ['STRICT_TRANS_TABLES',
            'NO_ZERO_IN_DATE',
            'NO_ZERO_DATE',
            'ERROR_FOR_DIVISION_BY_ZERO',
            'NO_AUTO_CREATE_USER',
            'NO_ENGINE_SUBSTITUTION'],

Or if you know what you are doing you can do directly (I won't do it):

'strict' => false,
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.