2013-01-22 8 views
6

Laravelで動作するようにPHP/MySQLを書き直しています。複数のテーブルからlaravelの流暢なクエリビルダを選択

SELECT p.post_text, p.bbcode_uid, u.username, t.forum_id, t.topic_title, t.topic_time, t.topic_id, t.topic_poster 
FROM phpbb_topics t, phpbb_posts p, phpbb_users u 
WHERE t.forum_id = 9 
AND p.post_id = t.topic_first_post_id 
AND u.user_id = t.topic_poster 
ORDER BY t.topic_time 
DESC LIMIT 10 

これはphpBBのフォーラムを照会し、記事を取得します: 私は再可能性がどのようにenter image description here

私がやりたいことの一つは、DBは、より簡潔with the Fluent Query Builderを問い合わせるが、私は少し失われたよ作るですFluent Query Builderの構文を利用するためにこれを書いてください。

答えて

18

テストしたが、ここで

return DB::table('phpbb_topics') 
         ->join('phpbb_posts', 'phpbb_topics.topic_first_post_id', '=', 'phpbb_posts.post_id') 
         ->join('phpbb_users', 'phpbb_topics.topic_poster', '=', 'phpbb_users.user_id') 
         ->order_by('topic_time', 'desc') 
         ->take(10) 
         ->get(array(

            'post_text', 
            'bbcode_uid', 
            'username', 
            'forum_id', 
            'topic_title', 
            'topic_time', 
            'topic_id', 
            'topic_poster' 


         )); 
+0

ああ、チェーンが参加し、あなたは可能性が認識されませんでした。役に立った、ありがとう。 - - –

+0

(5.3でテスト)以下のようなオプションがあります>(取得配列( 'phpbb_topics *'、 'ユーザ名' 英語を話していないために、 'phpbb_posts.post_text' ) – Sadat

2

このコードをしようと考えてみましょう開始されるわけではありません。あなたが必要とするものを完成させるはずです。

DB::select(DB::raw("SELECT p.post_text, p.bbcode_uid, u.username, t.forum_id, t.topic_title, t.topic_time, t.topic_id, t.topic_poster 
FROM phpbb_topics t, phpbb_posts p, phpbb_users u 
WHERE t.forum_id = 9 
AND p.post_id = t.topic_first_post_id 
AND u.user_id = t.topic_poster 
ORDER BY t.topic_time 
DESC LIMIT 10")); 
+1

は、あなたが診断されているように見えました問題は罰金:P – rayryeng

1
return DB::table(DB::raw('phpbb_topics t, phpbb_posts p, phpbb_users u')) -> select(DB::raw('p.post_text, p.bbcode_uid, u.username, t.forum_id, t.topic_title, t.topic_time, t.topic_id, t.topic_poster'))->where('phpbb_topics.topic_first_post_id', '=', 'phpbb_posts.post_id')->where('phpbb_users', 'phpbb_topics.topic_poster', '=', 'phpbb_users.user_id')->order_by('topic_time', 'desc')->take(10)->get(); 
関連する問題