Laravel 5.5 pagination example from scratch


Are you wanted to create pagination in your laravel 5.5 application ?, If Yes then you are a right place. Pagination in always require when we have to display table records. If you have thousands of records in table but you can't display at a time all the records so it's must be display per page some records. So here pagination will help to display per page something records.

If you are using core php then you have trouble to implement logic for pagination, but in laravel 5.5 version they provide very simple way to make pagination for your application. Here i will let you know very basic and simple example of pagination.

So, let' just follow three things to do pagination example:

Add Route:

First things is create route for simple pagination example, So let's open your web.php file and put bellow route:

routes/web.php

Route::get('mypagination', 'HomeController@myPagination');

Add Controller Method:

Now, we have to add myPagination() method in HomeController, in this method we write database query builder condition. So let's just add bellow method on home controller.

app/Http/Controllers/HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class HomeController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function myPagination()

{

$users = User::paginate(5);

return view('myPagination',compact('users'));

}

}

Add View File:

At last we will create blade file and display pagination layout using links() method, so you have to create blade file and put bellow code:

resources/views/myPagination.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 5.5 Pagination Example</title>

<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container">

<h2>Laravel 5.5 Pagination Example</h2>

<table class="table table-bordered">

<tr>

<th>Id</th>

<th>Name</th>

<th>Email</th>

</tr>

@if($users->count() > 0)

@foreach($users as $user)

<tr>

<td>{{ $user->id }}</td>

<td>{{ $user->name }}</td>

<td>{{ $user->email }}</td>

</tr>

@endforeach

@endif

</table>

{{ $users->links() }}

</div>

</body>

</html>

Now you can run this simple example.

I hope you found your best...


September 1, 2017 | 4654 Viewer | Category : PHP Laravel Bootstrap

Related Posts: