Laravel 对用户模型的原始查询

Laravel raw query to User Models

我有一个原始的 SQL-query,我想从中生成 Laravel Eloquent 个模型。

查询:

SELECT * FROM user
LEFT JOIN (SELECT user_id, MAX(log_login_id) AS maxid
FROM log_login
GROUP BY user_id) 
AS T1 ON user.user_id = T1.user_id
LEFT JOIN log_login AS T2 ON T2.log_login_id = T1.maxid
WHERE user.map_id = 101;

我正在尝试类似的方法,但效果不佳..

 $users = User::where('user.map_id', session('mapID'))
        ->leftJoin('(SELECT user_id, MAX(log_login_id) AS maxid
                   FROM log_login
                   GROUP BY user_id) 
                   AS T1 ON user.user_id = T1.user_id')
        ->leftJoin('log_login AS T2 ON T2.log_login_id = T1.maxid')
        ->orderBy('username', 'asc')->get();

如何设置查询,以便从 Eloquent 获取对象模型列表?我猜这是一个语法问题。

错误:

QueryException in Connection.php line 624:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`` left join `log_login` as `T2` on `` `` where `user`.`map_id` = ? order by `u' at line 1 (SQL: select * from `user` left join `(SELECT` as `MAX(log_login_id)` on `` `` left join `log_login` as `T2` on `` `` where `user`.`map_id` = 101 order by `username` asc)

in Connection.php line 624
at Connection->runQueryCallback('select * from `user` left join `(SELECT` as `MAX(log_login_id)` on `` `` left join `log_login` as `T2` on `` `` where `user`.`map_id` = ? order by `username` asc', array('101'), object(Closure)) in Connection.php line 580
at Connection->run('select * from `user` left join `(SELECT` as `MAX(log_login_id)` on `` `` left join `log_login` as `T2` on `` `` where `user`.`map_id` = ? order by `username` asc', array('101'), object(Closure)) in Connection.php line 297
at Connection->select('select * from `user` left join `(SELECT` as `MAX(log_login_id)` on `` `` left join `log_login` as `T2` on `` `` where `user`.`map_id` = ? order by `username` asc', array('101'), true) in Builder.php line 1377
at Builder->runSelect() in Builder.php line 1367
at Builder->getFresh(array('*')) in Builder.php line 1354
at Builder->get(array('*')) in Builder.php line 380

经过大量尝试和错误,这成功了:

$users = User::where('user.map_id', session('mapID'))
    ->leftJoin(DB::raw('(SELECT user_id,  MAX(log_login_id) AS maxid
       FROM log_login
       GROUP BY user_id) 
       AS T1'), function ($join) {
        $join->on("user.user_id", "=", "T1.user_id");
    })
    ->leftJoin(DB::raw('log_login AS T2'), function ($join) {
        $join->on('T2.log_login_id', '=', 'T1.maxid');
    })->get();

试试这个

$users = User::where('user.map_id', session('mapID'))
    ->leftJoin(\DB::raw('(SELECT user_id, MAX(log_login_id) AS maxid
               FROM log_login
               GROUP BY user_id) 
               AS T1 ON user.user_id = T1.user_id'))
    ->leftJoin('log_login AS T2 ON T2.log_login_id = T1.maxid')
    ->orderBy('username', 'asc')->get();

或这个(未测试但猜测它应该有效)

$users = User::where('user.map_id', session('mapID'))
    ->leftJoin(function($query){
       $query->select("user_id", "MAX(log_login_id) as maxid")
           ->from('log_login as T1')
           ->groupBy('user_id')
           ->on('user.user_id','=','T1.user_id')
    })
    ->leftJoin('log_login AS T2 ON T2.log_login_id = T1.maxid')
    ->orderBy('username', 'asc')->get();

希望对您有所帮助