vendredi 1 septembre 2017

Laravel 5.4 Eloquent ORM query triggers SQL Error 42000

I bought Laravel Socialite Script for Laravel 5.4 on CodeCanyon which consists in an entire system to get started with a social network. Since the customer support is Indian and their night shift is my day shift but I need I answer today I am here.

I get not one but three SQLSTATE[42000] errors when executing a Eloquent ORM query:

Errors

PHP Syntax:

/* 
 * $userId = "17"
 * Auth::id() = 1
 */

$recipients = [$userId, Auth::id()];
$recipients[1] = (string) Auth::id();

$thread = Thread::whereHas('participants', function ($query) use ($recipients) {
    $query->whereIn('user_id', $recipients)
        ->groupBy('thread_id')
        ->havingRaw('COUNT(thread_id)='.count($recipients));
})->first();

This generates this SQL Syntax when checking with toSQL() on the function:

select *
from `threads` 
where exists (
    select * 
    from `participants` 
    where `threads`.`id` = `participants`.`thread_id` 
    and `user_id` in (1, 17) 
    and `participants`.`deleted_at` is null 
    group by `thread_id` 
    having COUNT(thread_id)=2
) 
and `threads`.`deleted_at` is null

When I run the MySQL query on PHPMyAdmin I get:

1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'socialiteproddb.participants.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Any comments on this will be appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire