2013-01-22 47 views
6

我正在重寫一些PHP/MySQL以與Laravel一起工作。有一件事我會做的是使數據庫查詢更簡潔with the Fluent Query Builder,但我有點失落:用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

我怎麼能重新編寫這個以使用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();