2013-01-22 4 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(); 
관련 문제