iocod's avatar
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..

0 likes
11 replies
uxweb's avatar

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.

1 like
iocod's avatar
Level 13

Yes, i have tried that. But the database rows are really really high. in that case i have to get all data from database and paginate. i think that will make the search slower. So trying to find out better way..

1 like
pmall's avatar

Maybe a polymorphic model that references these three models ? then you can query the polymorphic model and display its related model.

your three tables seem very similar, maybe they need a little refactoring. Why is there three table with the same attributes ?

iocod's avatar
Level 13

That is just a sample table structure, only those fields comes common and there are more than 30 separate fields in each tables. There i no relation between them to do polymorphic relation. Usually how people doing to do a search similar to this?

pmall's avatar

Anyway you cant retrieve different types of model from a query. that's why a suggest a polymorphic model that contains all the similar fields from these three tables (name, etc...) and a polymorphic relation that links it to either Mobile, Television, or Watching.

1 like
HashmatWaziri's avatar

Solution: after 2 hours I found the solution as:

//routes.php

              Route::any('/all_games', function()
              {
              $all_games = DB::table('contents')->join('images', 'images.content_id', '=', 'contents.id')->paginate(4);
              return View::make('/all_games')->with("games_contents",$all_games);
              });

//all_games.blade.php

              <div class="container-fluid">
              <div class="caption">
              @foreach(array_chunk($games_contents->all(), 2) as $row)
              <div class="row">
                
               @foreach ($row as $item)
           
               <div class="col-xs-6">
               
                <div class="thumbnail">
                   
                <div class="img-rounded" >{{ $item->image_1 }}</div>
                      <br>
                       <strong id="game_name col-xs-12">{{$item->Name}}</strong> 
                       <br>
                       <p>Download the game with added values</p>
                       
                       <strong>Game Code: </strong>{{$item->code}}
                       <br>
                       <strong>RM: </strong>{{$item->price}}
                       <p><a href="#" class="btn btn-primary" role="button">Download</a></p>
                </div>

                  </div>      
              @endforeach
               </div>
              @endforeach
               </div>
     {{ $games_contents->links() }}
 </div>

Hope it helps you.

umeshsujakhu's avatar

I got the same problm.... can u please help me if you have solved it... ?? @ fasilkk

lostdreamer_nl's avatar
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.