tubbrez's avatar

Laravel 5.2 + Pagination + Multiple DB Table

Hi!

Does anybody have an idea of how to do pagination on a page whereby in a data table, i'm displaying data from multiple tables?

I've pasted below, the algorithms that i'm using and please note that it is working fine but now i want to add pagination to that page, so does anybody know how i could modify my controller or view so that i can achieve that?

Thank you in advance!

In my controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request; use App\Http\Controllers\Controller; use DB;

class DashboardController extends Controller {

public function index(Request $request) { $dataFirst = DB::table('firsts') ->join('account_connections', 'firsts.ftId', '=', 'account_connections.ftId')->get();

$dataSecond = DB::table('seconds')
    ->join('account_connections', 'seconds.trId', '=', 'account_connections.trId')->get();

$dataThird = DB::table('thirds')
    ->join('account_connections', 'thirds.geId', '=', 'account_connections.geId')->get();

 return view('dashboard')
            ->with('dataFirst', $dataFirst)
            ->with('dataSecond', $dataSecond )
            ->with('dataThird', $dataThird )
            ->with('i', ($request->input('page', 1) - 1) * 9);

}

}

In my view:

                    <thead>
                        <tr>
                            <th>No</th>
                            <th>My Team</th>
                            <th>Website</th>
                        </tr>
                    </thead>
                    
                    @foreach($dataFirst as $dataFirst)
                    @if(Auth::user()->id == $dataFirst->user_id)
                    
                    <tbody>
                        <tr>
                            <td>{{ ++$i }}</td>
                            <td>{{ $dataFirst->first_name}} {{ $dataFirst->last_name}}</td>
                            <td>{{ $dataFirst->website}}</td>
                        </tr>
                    </tbody>
                    
                    @endif
                    @endforeach
                    
                    @foreach($dataSecond as $dataSecond )
                    @if(Auth::user()->id == $dataSecond ->user_id)
                    
                    <tbody>
                        <tr>
                            <td>{{ ++$i }}</td>
                            <td>{{ $dataSecond ->first_name}} {{ $dataSecond ->last_name}}</td>
                            <td>{{ $dataSecond ->website}}</td>
                        </tr>
                    </tbody>
                    
                    @endif
                    @endforeach
                    
                    @foreach($dataThird as $dataThird)
                    @if(Auth::user()->id == $dataThird->user_id)
                    
                    <tbody>
                        <tr>
                            <td>{{ ++$i }}</td>
                            <td>{{ $dataThird->first_name}} {{ $dataThird->last_name}}</td>
                            <td>{{ $dataThird->website}}</td>
                        </tr>
                    </tbody>
                    
                    @endif
                    @endforeach 
                    
                  
0 likes
2 replies
ediblemanager's avatar
Level 3

Instead of retrieving the collection using ->get(), you'd need to use ->paginate() and then in your view, {{ $datafirst->links() }}.

This assumes that all the data from the DB is within the one collection, so you might want to put all the joins together into a single statement:

$data = DB::table('seconds')
    ->join('account_connections', 'seconds.trId', '=', 'account_connections.trId')
    ->join('account_connections', 'thirds.geId', '=', 'account_connections.geId')
->where{ ... }
->get();

Also, see here for a SO example which also includes using models.

1 like

Please or to participate in this conversation.