laravel - 有没有办法使用查询构建器简单地对多个表进行选择?
问题描述
我想从按创建日期排序的 2 个表中获取结果。
有没有办法使用 Laravel 查询生成器然后获取 Collection ?
它看起来很基本,但我找不到如何实现这一点。
提前感谢您的帮助。
$articles = DB::table(['news', 'posts'])->orderBy('created_at')->get();
解决方案
您无法在单个集合中获取两个表的集合
但是您可以将两个集合合并为一个。
$posts = DB::table('posts')->orderBy('created_at')->get();
$news = DB::table('news')->orderBy('created_at')->get();
$articles = $posts->merge($news);
推荐阅读
- r - 将多个 .txt 文件合并到一个数据框中
- python - Windows 10 for Linux 环境下的 Python 编程
- bash - 如何用gpg检查发布软件的签名?
- python - Python Django - 在网页上显示所有分配的对象
- angular - expect(component).toBeTruthy() 失败
- python - FT2232h/FT42232h 兼容全双工串行监视器
- bash - 如何在给定模式后剪切文件中的字符串?
- python-3.x - 从python中的文件开头删除不固定数量的行
- kubernetes - 如何将 Kubernetes 主节点从一台服务器移动到另一台服务器?
- elasticsearch - elasticsearch 7和映射和排序