简体   繁体   中英

How to search in two different tables?

Here is my current queries:

$q = $request->q;
-- first table
$n_arr = news::where('title','like',$q)->orWhere('description','like',$q)->get();
-- second table
$p_arr = productions::where('title','like',$q)->orWhere('description','like',$q)->get();

as you see, currently I do that by two different queries. But now I need to use ->paginate() , So I need a single query to paging all results as well. I need something like union clause.

In other word, I need to combine the result of those two queries above.

How can I do that in Laravel?

One way to do that is to create pagination manually .

Another way to do that is to use skip() and take() methods in both queries to get results for current page.

$n_arr = news::where('title','like',$q)->orWhere('description','like',$q)->->skip($page * $perPage)->take($perPage)get();
$p_arr = productions::where('title','like',$q)->orWhere('description','like',$q)->skip($page * $perPage)->take($perPage)->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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