繁体   English   中英

在laravel中结合两个不同的没有关系的数据库表查询进行分页

[英]Combining two different with no relationship DB table query in laravel for pagination

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

这是我的代码。 newsseason表不相关,但它们有类似的列,我试图将它们放在一个对象中以便于分页。 是否可以?

$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 语句合并为一个语句。 然后你可以应用全局orderBypaginate

$news = DB::table("news")
    -> ...
    ->orWhere(...);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM