mangoz's avatar

Get db info with pagination

I use laravel 5 and i want to fetch some data from db (while loop) I want to show in blade template with pagination. I have controller and i want to put all code there. (in public function)

How to do that ? Please, give me full example i'm newbie.

0 likes
1 reply
dede02830's avatar

In your controller you need a query with paginate :

public function index() {
        $titre = "Network points";
        $titreinfos = DotsController::TITREINFOS;
        $infos = DotsController::INFOS;
        $plansChoix = \DB::table('Plan')->lists('name', 'planID');
        $name = '';
        $networkLabel = '';
        $typeLabel = '';

        $points = \DB::table('Point')
                ->leftjoin('Network', 'Network.networkID', '=', 'Point.networkID')
                ->leftjoin('TPoint', 'TPoint.tpointID', '=', 'Point.tpointID')
                ->orderBy('Point.pointID', 'asc')
                ->paginate(30, array('Point.pointID', 'Point.active', 'Point.name', 'Network.label as netlab', 'TPoint.label as tplab'));
        
        $networks = \DB::table('Network')->lists('label', 'networkID');
        $types = \DB::table('TPoint')->lists('label', 'tpointID');       
        
        return view('networkDots', compact('points', 'networks', 'types', 'titre', 'infos', 'titreinfos', 'plansChoix', 'name', 'networkLabel', 'typeLabel'));
    }

after this, you need in your view :

{!! str_replace('/?', '?', $points->render()) !!}

for exemple :

...
<table class="table table-striped mon-tab-avec-photo">
                <thead>
                    <tr>
                        <th class="noMobile">#</th>
                        <th>Name</th>
                        <th class="noMobile">Net</th>
                        <th class="noMobile">Type</th>
                        <th class="noMobile">Acti</th>
                        <th>Infos</th>
                    </tr>
                </thead>

                <tbody>
                    
                   
                    @foreach($points as $point)
                    <tr>
                        <td class="zonePourLaCouleur noMobile">{{ $point->pointID }}</td>
                        <td class="zonePourLaCouleurMobile">{{ $point->name }}</td>
                        <td class="noMobile">{{ $point->netlab }}</td>
                        <td class="noMobile">{{ $point->tplab }}</td>
                        <td class="noMobile">
                            @if($point->active == 0)
                                {!! HTML::linkAction('DotsController@activate', 'Activer', array($point->pointID), array('class' => 'buttonActiveDesa')) !!}
                            @else
                                {!! HTML::linkAction('DotsController@activate', 'Desactiver', array($point->pointID), array('class' => 'buttonActive')) !!}
                            @endif
                        </td>
                        <td>{!! HTML::linkAction('DotsController@infos', ' ', array($point->pointID), array('class' => 'buttonEdit')) !!}</td>
                    </tr>
                    @endforeach
 
                </tbody>

            </table>

{!! str_replace('/?', '?', $points->render()) !!}
...

this create pagination, you need pass $points in your view and use it for the pagination.

good luck ;)

Please or to participate in this conversation.