简体   繁体   中英

Laravel 5.4 query builder having issues with groupBy

I'm trying to convert this query to query builder in Laravel 5.4:

SELECT
    oc.id,
    oc.name,
    oat.user_id,
    p.first_name,
    p.last_name
FROM
    oauth_clients oc
    LEFT JOIN oauth_access_tokens oat ON oc.id = oat.client_id
    JOIN users u on u.id = oat.user_id
    JOIN people p on p.id = u.person_id
WHERE oc.revoked = false AND oc.password_client = true
GROUP BY oc.id, oat.user_id

And getting this error barf: Argument 1 passed to Illuminate\\Database\\Connection::prepareBindings() must be of the type array, string given, called in /var/www/html/source/luniverse/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 648 and defined This is my attempt at it (one of many):

$tokens = DB::select('oc.id','oc.name','oat.user_id','p.first_name','p.last_name')
                ->from('oauth_clients as oc')
                ->leftJoin('oauth_access_tokens as oat', 'oc.id', '=', 'oat.client_id')
                ->join('users as u', 'u.id', '=', 'oat.user_id')
                ->join('people as p', 'p.id', '=', 'u.person_id')
                ->where('oc.revoked', '=', 'false')
                ->where('oc.password_client', '=', 'true')
                ->groupBy('oc.id')
                ->groupBy('oat.user_id')
                ->get();

The database config is set to strict mode, but that doesn't exactly seem to explain that particular error. The raw query runs fine in a DB gui.

Change the groupby code like

$tokens = DB::select('oc.id','oc.name','oat.user_id','p.first_name','p.last_name')
            ->from('oauth_clients as oc')
            ->leftJoin('oauth_access_tokens as oat', 'oc.id', '=', 'oat.client_id')
            ->join('users as u', 'u.id', '=', 'oat.user_id')
            ->join('people as p', 'p.id', '=', 'u.person_id')
            ->where('oc.revoked', '=', 'false')
            ->where('oc.password_client', '=', 'true')
            ->groupBy('oc.id','oat.user_id')
            ->get();

DB::select() executes a query, you have to use DB::table() :

DB::table('oauth_clients as oc')
    ->select('oc.id','oc.name','oat.user_id','p.first_name','p.last_name')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM