在laravel中将两个不同且没有关系的DB表查询结合起来以进行分页

编程入门 行业动态 更新时间:2024-10-23 23:19:48
本文介绍了在laravel中将两个不同且没有关系的DB表查询结合起来以进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的数据库中有两个不相关的表,我需要将它们合并,以便可以将其放在搜索视图中,但我不知道是否可行.

I have two tables in my database that are not related, and I need those to be merge so I can put it on my search view but I don't have any idea, if it is possible or not.

这是我的代码. news和season表没有关联,但是它们具有相似的列,我试图将它们放在一个对象中以便于分页.有可能吗?

Here is my code. The news and season tables are not related but they have similar columns which I am trying to put in one object for easy pagination. Is it possible?

$search = $request->search; $nresult = DB::table("news") ->select("news.news_id as id","news.title as title", "news.name_key as key", "news.content", "news.image", "news.thumb", DB::raw("'news' as type")) ->where("news.status", "=", 1) ->Where("news.title", "like", "%".$search."%") ->orWhere("news.content", "like", "%".$search."%") ->orWhere("news.variables", "like", "%".$search."%") ->orWhere("news.categories", "like", "%".$cat_id."%") ->orderBy("title", "asc") ->paginate(5); $sresult = DB::table("season") ->select("season.season_id as id","season.name as title", "season.key as key", "season.content", "season.image", "season.thumb", DB::raw("'season' as type")) ->where("season.status", "=", 1) ->Where("season.name", "like", "%".$search."%") ->orWhere("season.content", "like", "%".$search."%") ->orWhere("season.variables", "like", "%".$search."%") ->orderBy("title", "asc") ->paginate(5);

推荐答案

您可以使用union运算符将两个select语句组合为一个语句.然后,您可以应用全局orderBy和paginate:

You can combine two select statements into one statement with the union operator. Then you can apply a global orderBy and paginate:

$news = DB::table("news") -> ... ->orWhere(...); $result = DB::table("season") -> ... ->orWhere(...) ->union($news) ->orderBy("title") ->paginate(10);

更多推荐

在laravel中将两个不同且没有关系的DB表查询结合起来以进行分页

本文发布于:2023-10-28 18:29:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537428.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   结合起来   中将   两个   关系

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!