Laravel 5.4查询构建器与groupBy有问题

时间:2018-04-03 17:31:14

标签: php mysql laravel eloquent query-builder

我正在尝试将此查询转换为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

获得此错误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 这是我的尝试(许多之一):

$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();

数据库配置设置为严格模式,但这似乎并不能解释该特定错误。原始查询在DB gui中运行正常。

2 个答案:

答案 0 :(得分:1)

更改groupby代码,如

$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();

答案 1 :(得分:0)

DB::select()执行查询,您必须使用DB::table()

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