You can build a brand new Illuminate\Pagination\Paginator and pass an array of all the items coming from all the search done in different tables. It is just an idea, I have not tried it.
Oct 18, 2014
11
Level 13
How can i do Laravel pagination for multiple table results(No relations)?.(i need this function to show search results from multiple tables)
$mobiles=Mobile::select('id','name','blurb','priority')
->where('name','LIKE','%'.$key.'%')
->paginate(3);
$televisions=Television::select('id','name','blurb','priority')
->where('name','LIKE','%'.$key.'%')
->paginate(3);
$washingmachines=Waching::select('id','name','blurb','priority')
->where('name','LIKE','%'.$key.'%')
->paginate(3);
This is the code, I want to mix all order by priority. and i want to show it in a single page with pagination. Or is the any alternative way? Sorry for the direct question..
Level 53
Use the query builder, select from all 3 tables at once (using UNION) and use paginate() on the result.
Shouldnt be any problem to handle this from the query builder without having to get all results first.
Untested:
$mobiles = Mobile::select('id','name','blurb','priority')->where('name','LIKE','%'.$key.'%');
$televisions = Television::select('id','name','blurb','priority')->where('name','LIKE','%'.$key.'%');
$data = Waching::select('id','name','blurb','priority')->where('name','LIKE','%'.$key.'%')
->union($mobiles)
->union($televisions)
->orderBy('priority')
->paginate(3);
10 likes
Please or to participate in this conversation.