How To Add Pagination In Laravel

How To Add Pagination In Laravel-Programming Pot
••• How To Add Pagination In Laravel

In this tutorial, you can learn How To Add Pagination In Laravel blade file. If you are beginners then it can help you. Laravel 5 provides its own method for pagination. But if you are work on code PHP then you have to write long code for pagination and render view etc but in Laravel, it is very simple so let’s select data following way in Controller.

How To Add Pagination In Laravel


In other frameworks, pagination can be very painful. Laravel makes it a breeze. Laravel can generate an intelligent “range” of links based on the current page. The generated HTML is compatible with the Bootstrap CSS framework.

Use

There are several ways to paginate items. The simplest is by using the method paginate on the query builder or an Eloquent model.

1. Paginating Database Results

$users = DB::table('users')->paginate(15);

2. Creating A Paginator Manually

Sometimes you may wish to create a pagination instance manually, passing it an array of items. You may do so by creating either an Illuminate\Pagination\Paginator or Illuminate\Pagination\LengthAwarePaginator instance, depending on your needs.

Paginating An Eloquent Model

You may also paginate Eloquent models:

$allUsers = User::paginate(15);

$someUsers = User::where('votes', '>', 100)->paginate(15);

The argument passed to the paginate method is the number of items you wish to display per page. Once you have retrieved the results, you may display them in your view, and create the pagination links using the render method:

<div class="container">
    <?php foreach ($users as $user): ?>
        <?php echo $user->name; ?>
    <?php endforeach; ?>
</div>

<?php echo $users->render(); ?>

This is all it takes to create a pagination system! Note that we did not have to inform the framework of the current page. Laravel will determine this for you automatically.

You may also access additional pagination information via the following methods:

  • currentPage
  • lastPage
  • perPage
  • hasMorePages
  • url
  • nextPageUrl
  • firstItem
  • lastItem
  • total
  • count

3. Simple Pagination

If you are only showing “Next” and “Previous” links in your pagination view, you have the option of using the simplePaginate method to perform a more efficient query. This is useful for larger datasets when you do not require the display of exact page numbers in your view:

$someUsers = User::where('votes', '>', 100)->simplePaginate(15);

4.Controller File

You can use the pagination in directly controller file also

public function getUser()
{
    $users = User::paginate(10);
    return view('users.index',compact('users'));
}

In above example you can see how it will return pagination with 10 records if you want to 5 or 15 etc then you can change argument. Now we have to render pagination view so, let’s see.

Blade File:

@extends('master.app')
@section('content')
<table class="table table-bordered">
    <thead>
        <tr>
            <th>Name</th>
            <th width="300px;">Action</th>
        </tr>
    </thead>
    <tbody>
        @if(!empty($users) && $users->count())
		    @foreach($users $key => $user)
		        <tr>
		            <td>{{ $user->name }}</td>
		            <td>
		                <button class="btn btn-danger">Delete</button>
		            </td>
		        </tr>
		    @endforeach
		@else
		    <tr>
		       <td colspan="10">There are no data.</td>
		   </tr>
	@endif
    </tbody>
</table>
{{ $user->render() }}
@endsection

In this ways How To Add Pagination In Laravel, we can use the pagination in the laravel framework. if have any problem then feel free to ask to go below and comment your queries.

Laravel Simple CRID Operation.