How to create Pagination in Laravel

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this tutorial you can learn about how to add simple pagination in our blade file. If we are a beginners then i hope it will help you. Laravel provide us it’s own method for the pagination. But if we are work on code PHP then we need to write the long code for the pagination and render view etc but now in Laravel it is very simple.

So let’s select the data by using the following way in Controller.

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

For the above example we can see now it will return us the pagination also with 10 records if we want to 5 or 15 etc then we can the change argument.

Now we have to render the pagination view so, let’s start and see.

Blade File:
@extends($theme)


@section('content')


<table class="table table-bordered">
    <thead>
        <tr>
            <th>Name</th>
            <th width="300px;">Action</th>
        </tr>
    </thead>
    <tbody>
        @if(!empty($data) && $data->count())
		    @foreach($data as $key => $value)
		        <tr>
		            <td>{{ $value->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>


{!! $data->render() !!}


@endsection

Read Also : How to convert Object into Array in PHP?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →