Quick Contact

    Pagination in Laravel

    Let’s learn about pagination. Pagination means that when you have the thousands of data and you want to show it in chunks. Suppose you have thousands of data and you want to show 100 data on each page, because if you just want to show the old data on single page then that is not a proper readable format and your page will take too much time to load. So, that’s why pagination is needed.

    – So first you need to make model, controller, and view and after that you will fetch the data from the database and show the data with pagination.

    – Suppose you have database with some particular name and table in which there are number of records and break into chunks of records so that you can use the pagination

    1. So first make a view file- go to view folder and create a file as “list.blade.php” and write in that as-:
    2. Members List

    3. Go to console and create controller-:
    4. Php artisan make:controller MembersController (controller name)

      And enter and controller will be created.

    5. In controller folder you will find the controller named file.
    6. Let’s make a function inside it and function name can be like “show ()” so code is as-:

  • Let’s simply make a route, so go for routes folder and create a file “web.php”and in that import the path for members controller as-:
  • Use App\Http\Controllers\MembersController;

    Now use the get method for the route class and all as-:

    Route::get (‘list’, [MembersController :: class, ‘show’]);

  • Also return list in MembersController.php file

    Let’s go to browser and run the project to see the output-:

    And it’s working fine.

    -Now define the model, as here table name is included. Because model is plural of table so to define model the database table name is included. So open console and write “php artisan make:model member” and enter and model file in created in model folder.

    - Open model.php file from studio.

    Note-: One more thing to know you that if table name and model name is not matched then in model file write-:

    Public $table=”t_name”

    - So first include the path of model file in controller i.e. “memberscontroller.php”

    Use App\Models\Member;

    And store the data in variable and pass in second parameter in array form as-:

      Function show ()
     $data = Member::all ();
       Return view (‘list’, [‘members’=> $data]);

    Now go to view file i.e. “list.blade.php” and create a table here and write the code as-:

    < h1> Members List < /h1>
    < table border=”1”>
       @foreach ($members as $member)
    < tr>
       < td>  ID < /td>
       < td> Name< /td>
       < td> Email< /td>
       < td> Address< /td>
       < /tr>
       < /table>

    Now if you go to browser and run the project then you will get the list as-:

    As dynamic table is needed so write-:

    So go to view file “list.blade.php” and put id’s in table data.

      < tr> 
    < td> {{$member[‘id’]}}< /td>
    < td> {{$member[‘name’]}}
    < td> {{$member[‘email’]}}
    < td> {{$member[‘address’]}}
    < /tr>

    Now go to browser and check you will get the table data.

    Now how you can apply pagination here.

    - As you can see that all records are visible so let’s open controller file and make changes for pagination as-:

    Put paginate and put the value for how much values you want to show on one page as-:

    $data = Member:: paginate (5);

    - Now you also have to access to other pages for other data so go to view file and write at the bottom of you page-:

    < span>
          {{$members-> links()}}
    < /span>
    Alongwith this adjusts the design of page with css in views file only in the end as-:
    < style>
          Display: none
      < /style>

    Now the output is as-:

    Now you can check the access of other pages by pressing next button.


    Apply now for Advanced PHP Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.