使用laravel fluent查询生成器从多个表中进行选择

Sim*_*ith 10 php mysql laravel

我正在重写一些PHP/MySQL来与Laravel一起工作.我想做的一件事是使用Fluent Query Builder使数据库查询更简洁,但我有点迷失:

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
Run Code Online (Sandbox Code Playgroud)

这查询phpbb论坛并获取帖子: 在此输入图像描述

我怎么能重写这个来使用Fluent Query Builder语法?

ita*_*chi 22

没有测试,但这是一个开始

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'
    ));
Run Code Online (Sandbox Code Playgroud)


小智 5

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();
Run Code Online (Sandbox Code Playgroud)