2

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?

stack
  • 10,280
  • 19
  • 65
  • 117

1 Answers1

0

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();
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • well still there is two results: both `$n_arr` and `$p_arr` .. Should I use `->render()` on which one? – stack Oct 25 '16 at 13:01
  • Still I'm trying to fix this problem ... I really don't know how should I use paginate on two different queries. – stack Oct 26 '16 at 13:12
  • @stack, you should `render()` only if you're craeting `Paginator` object manually. If you're using `skip()` and `take()`, you should create links manually without `render()` – Alexey Mezenin Oct 26 '16 at 13:14