Laravel 10: Paginate DB Table Data with Bootstrap Tutorial

In this tutorial, you will learn to create a simple web application with Pagination using Laravel 10 (PHP Framework) and Bootstrap Framework. The tutorial aims to provide students and beginners with a reference for learning some useful web application components using Laravel. Here, I will be providing the steps and scripts for creating a simple web application that demonstrates pagination using Laravel.

What is Pagination?

Pagination is one of the useful and often used components in web applications. This component limits the number of data to display and provides links such as next, previous, and specific pages to load the limited number of data on the specific page.

Laravel (PHP Framework) comes with a Pagination approach that allows the developers to implement this kind of feature or component easily. The said framework paginator is integrated with the query builder and eloquent ORM.

In this tutorial, I will be using Laravel version 10.8 and Bootstrap 5 Framework.

Getting Started

In this tutorial, we will need the following software to create and run a Laravel Project on our local machine or devices. Kindly download and install the following.

  • Code Editor[ i.e. VS Code Editor, Sublime, Notepad++, etc ]
  • XAMPP or any equivalent
  • Composer

After the successful download and installation, make sure to run your MySQL server for the database. If you are using XAMPP, you can also start the Apache server so you can also access the PHPMyAdmin front end.

Creating the Laravel Project

Open your terminal or command prompt and change the current directory to your XAMPP htdocs directory. If you are using other software change it into the equivalent directory. After that, run the following composer script to create a Laravel project, download the dependencies, and install it.

composer create-project laravel/laravel laravel-pagination

Laravel 10: Paginate DB Table Data with Bootstrap Tutorial

A new directory will be created named laravel-pagination. Change your terminal or command prompt directory to this folder.

cd laravel-pagination

Creating the Database

Open your preferred browser and browse the PHPMyAdmin (i.e. http://localhost/phpmyadmin). Next, create a new database named dummy_db.

After that, open the .env file from the root folder with your preferred code editor and update database credentials such as the database host, name, password, and username.

Next, run the following script in your terminal or command prompt.

php artisan make:model Members --resource -m

Laravel 10: Paginate DB Table Data with Bootstrap Tutorial

The script will create a new Model, Controller, and Migration files.

Paths:

  • app/models/Members.php
  • app/Http/Controllers/MembersController.php
  • database/migration/<date_time>_create_members_table.php

Next, open the <date_time>_create_members_table.php file in your code editor and update the content like the following.

  1. <?php
  2.  
  3. use Illuminate\Database\Migrations\Migration;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Support\Facades\Schema;
  6.  
  7. return new class extends Migration
  8. {
  9. /**
  10.   * Run the migrations.
  11.   */
  12. public function up(): void
  13. {
  14. Schema::create('members', function (Blueprint $table) {
  15. $table->id();
  16. $table->string('name');
  17. $table->string('email');
  18. $table->string('phone');
  19. $table->timestamps();
  20. });
  21. }
  22.  
  23. /**
  24.   * Reverse the migrations.
  25.   */
  26. public function down(): void
  27. {
  28. Schema::dropIfExists('members');
  29. }
  30. };
  31.  

The script above will create a database table named members which will be used for storing and retrieving the sample data of the application.

Next, execute migrate command in your terminal or command prompt. To run the database tables creation.

php artisan migrate

Laravel 10: Paginate DB Table Data with Bootstrap Tutorial

Inserting Sample Random Data

The next step is we'll be populating the members table with some sample random data using the Database Seeder and Factory. To do that, run the following command.

php artisan make:seeder MembersSeeder

Laravel 10: Paginate DB Table Data with Bootstrap Tutorial

And

php artisan make:factory MembersFactory

Laravel 10: Paginate DB Table Data with Bootstrap Tutorial

Next, update the created factory file content located in database/factories directory like the following:

  1.  
  2. <?php
  3.  
  4. namespace Database\Factories;
  5.  
  6. use Illuminate\Database\Eloquent\Factories\Factory;
  7.  
  8. /**
  9.   * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Members>
  10.   */
  11. class MembersFactory extends Factory
  12. {
  13. /**
  14.   * Define the model's default state.
  15.   *
  16.   * @return array<string, mixed>
  17.   */
  18. public function definition(): array
  19. {
  20. return [
  21. 'name' => fake()->name(),
  22. 'email' => fake()->unique()->safeEmail(),
  23. 'phone' => fake()->phoneNumber()
  24. ];
  25. }
  26. }
  27.  

The file above defines the random data to create each column of the database members table.

Next, update the created seeder file content located in database/seeders directory like the following:

  1. <?php
  2.  
  3. namespace Database\Seeders;
  4.  
  5. use App\Models\Members;
  6. use Illuminate\Database\Console\Seeds\WithoutModelEvents;
  7. use Illuminate\Database\Seeder;
  8.  
  9. class MembersSeeder extends Seeder
  10. {
  11. /**
  12.   * Run the database seeds.
  13.   */
  14. public function run(): void
  15. {
  16. Members::factory()
  17. ->count(150)
  18. ->create();
  19. }
  20. }
  21.  

The file will execute the insertion of random data on the database upon running the following command:

run php artisan db:seed MembersSeeder

Laravel 10: Paginate DB Table Data with Bootstrap Tutorial

After that, you can check if the data are successfully inserted into the database through PHPMyAdmin.

Creating the Controller

Next, we will update the controller file for retrieving the data from the database and paginate it limited to 10 data rows per page. Open the apps/Http/Controllers/MembersController.php file on your code editor and update the content like the following.

  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Models\Members;
  6. use Illuminate\Http\Request;
  7.  
  8. class MembersController extends Controller
  9. {
  10. /**
  11.   * Display a listing of the resource.
  12.   */
  13. public function index()
  14. {
  15. //
  16. $members = Members::paginate(10);
  17. return view('members', ['members'=>$members]);
  18. }
  19.  
  20. /**
  21.   * Show the form for creating a new resource.
  22.   */
  23. public function create()
  24. {
  25. //
  26. }
  27.  
  28. /**
  29.   * Store a newly created resource in storage.
  30.   */
  31. public function store(Request $request)
  32. {
  33. //
  34. }
  35.  
  36. /**
  37.   * Display the specified resource.
  38.   */
  39. public function show(Members $members)
  40. {
  41. //
  42. }
  43.  
  44. /**
  45.   * Show the form for editing the specified resource.
  46.   */
  47. public function edit(Members $members)
  48. {
  49. //
  50. }
  51.  
  52. /**
  53.   * Update the specified resource in storage.
  54.   */
  55. public function update(Request $request, Members $members)
  56. {
  57. //
  58. }
  59.  
  60. /**
  61.   * Remove the specified resource from storage.
  62.   */
  63. public function destroy(Members $members)
  64. {
  65. //
  66. }
  67. }
  68.  

Creating the Page Interface

Next, will create the web application sample page. To do that create a new blade file named members.blade.php inside the resources/views directory. Then paste the following script.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Sample Data Pagination</title>
  7. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
  8.  
  9. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  10. </head>
  11. <div class="container-md py-5">
  12. <div class="table-responsive">
  13. <table class="table table-sm table-bordered table-striped">
  14. <tr>
  15. <th>ID</th>
  16. <th>Name</th>
  17. <th>Email</th>
  18. <th>Phone</th>
  19. </tr>
  20. </thead>
  21. @foreach($members as $member)
  22. <tr>
  23. <td class="text-center">{{$member->id}}</td>
  24. <td>{{$member->name}}</td>
  25. <td>{{$member->email}}</td>
  26. <td>{{$member->phone}}</td>
  27. </tr>
  28. @endforeach
  29. </tbody>
  30. </table>
  31. {{$members->onEachSide(2)->links()}}
  32. </div>
  33. </div>
  34. </body>
  35. </html>

The file script above contains the HTML elements of the page layout, data table, and pagination links. The Bootstrap v5 Framework is loaded on the page using CDNs which means that an internet connection is a must when browsing the page later on.

Using Bootstrap for the Paginator Links

To use the Bootstrap 5 pagination links design, we must enable the use of the Paginator Bootstrap 5 service provider. To do that, open the app/Providers/AppServiceprovider.php file on your code editor and update the content like the following:

  1. <?php
  2.  
  3. namespace App\Providers;
  4.  
  5. use Illuminate\Support\ServiceProvider;
  6. use Illuminate\Pagination\Paginator;
  7.  
  8. class AppServiceProvider extends ServiceProvider
  9. {
  10. /**
  11.   * Register any application services.
  12.   */
  13. public function register(): void
  14. {
  15. //
  16. }
  17.  
  18. /**
  19.   * Bootstrap any application services.
  20.   */
  21. public function boot(): void
  22. {
  23. //
  24. Paginator::useBootstrapFive();
  25. }
  26. }

Update the Routing File

Finally, update the web application routing configuration. To do that, open the routes/web.php and comment on the default route script and add the controller route script like the following:

  1. <?php
  2.  
  3. use Illuminate\Support\Facades\Route;
  4. use App\Http\Controllers\MembersController;
  5.  
  6. /*
  7. |--------------------------------------------------------------------------
  8. | Web Routes
  9. |--------------------------------------------------------------------------
  10. |
  11. | Here is where you can register web routes for your application. These
  12. | routes are loaded by the RouteServiceProvider and all of them will
  13. | be assigned to the "web" middleware group. Make something great!
  14. |
  15. */
  16.  
  17. // Route::get('/', function () {
  18. // return view('welcome');
  19. // });
  20. Route::controller(MembersController::class)->group(function(){
  21. Route::get('/', 'index');
  22. });

Then, we can now run the Laravel Project and browse it to the browser and see if it works and meets our objective in this tutorial. Run the following command:

php artisan serve

Browse http://127.0.0.1:8000 on your browser.

The web page must result in something like the following snapshot:

Laravel 10: Paginate DB Table Data with Bootstrap Tutorial

Laravel 10: Paginate DB Table Data with Bootstrap Tutorial

There you go! I have also provided the complete source code zip file that I created for this tutorial on this website and it is free to download. The download button is located below this tutorial's content. Feel free to download and modify the source code the way you wanted to enhance your programming capabilities using Laravel 10.

How to Run the source code?

  • Please start the Apache and MySQL servers of your XAMPP or equivalent software.
  • Extract the source code folder into the XAMPP's htdocs directory or equivalent directory.
  • Create a new database named dummy_db
  • Open the terminal or command prompt and redirect the directory to the source code folder
  • Run the php artisan migrate command
  • Run the php artisan make:seeder MembersSeeder command
  • Run the php artisan serve command
  • Browse the application. http://127.0.0.1:8000

That's it! I hope this Laravel 10: Paginate DB Table Data with Bootstrap Tutorial will help you with what you are looking for and that you'll find this useful for your current and future PHP Projects using Laravel 10 Framework.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment