2017-03-24 15 views
0

私はLaravel Eloquentモデルを作成したい生のSQLクエリを持っています。ユーザーモデルへのLaravel生のクエリ

問合せ:

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; 

イム..このような何かをしようとしますが、私はかなりの仕事をdoesntの

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

どのように私のセットアップ私のクエリに、私は、オブジェクトモデルのリストからを取得するように、雄弁ですか?これは構文上の問題です。

エラー:

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 

試行錯誤をたくさんした後、このトリックでした:

​​
+0

を役に立てば幸い? –

+0

はい、実際に私は – boomdrak

答えて

2

この

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

あなたはlog_login` `用に設定雄弁モデルを持っています、それは

+0

ありがとうありがとうございます:) – boomdrak

関連する問題