使用laravel fluent查询生成器从多个表中选择 [英] Select from multiple tables with laravel fluent query builder

查看:39
本文介绍了使用laravel fluent查询生成器从多个表中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重新编写一些 PHP/MySQL 以使用 Laravel.我想做的一件事是使 DB 查询更简洁使用 Fluent Query Builder 但我我有点失落:

I'm re-writing some PHP/MySQL to work with Laravel. One thing I would like to do is make the DB queries more succinct with the Fluent Query Builder but I'm a bit lost:

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 论坛并获取帖子:

This queries a phpbb forum and gets posts:

我如何重写它以使用 Fluent Query Builder 语法?

How could I re-write this to make use of the Fluent Query Builder syntax?

推荐答案

未测试但这是一个开始

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'
    ));

这篇关于使用laravel fluent查询生成器从多个表中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆