首页 > 解决方案 > 有没有办法使用查询构建器简单地对多个表进行选择?

问题描述

我想从按创建日期排序的 2 个表中获取结果。

有没有办法使用 Laravel 查询生成器然后获取 Collection ?

它看起来很基本,但我找不到如何实现这一点。

提前感谢您的帮助。

$articles = DB::table(['news', 'posts'])->orderBy('created_at')->get();

标签: laravel

解决方案


您无法在单个集合中获取两个表的集合

但是您可以将两个集合合并为一个。

$posts = DB::table('posts')->orderBy('created_at')->get();


$news = DB::table('news')->orderBy('created_at')->get();

$articles = $posts->merge($news);

推荐阅读