本文介绍了在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表查询结合起来以进行分页
发布评论