Laravel 5 Pagination using LengthAwarePaginator

Laravel 5 have a rich library to develop web application.Similar pagination in laravel have very fantastic features to manage records in listing, there is two class to load into your controller for render pagination in views which are following which will help to develop Pagination:

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

class LengthAwarePaginator extends AbstractPaginator and implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable,LengthAwarePaginator (view source)
class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable (View source)
Now we will start Laravel 5 Pagination using LengthAwarePaginator and Collaction.
First we need to create model to fetch database record here we will fetch record from blog table (Please view my previous tutorial to connect database and fetch records and create table) and define primary key and timestamp false and define all column variable.

BlogModel.php
<?php 
namespace 'According to your directory structure';
use Illuminate\Database\Eloquent\Model;
use DB;
class BlogModel extends Model
{
	/**
	 * Added just to demonstrate that models work
	 * @return String
	 */
	protected $table = 'blog';
        protected $primaryKey = 'blog_id';
        protected $fillable   = ['blog_id','blog_name', 'blog_desc'];
        protected $blog_id = 0;
        protected $blog_name = '';
        protected $blog_desc = '';
        public $timestamps = false;
}

Now you have to create controller for pass fetched data to view with pagination.

<?php 
BlogController.php
namespace 'According to your directory structure';

use App\Http\Controllers\Controller;
use App\Modules\Blog\Models\BlogModel; Note -> 'According to your directory structure';
use Illuminate\Support\Facades\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

/**
 * BlogController
 *
 * Controller to house all the functionality directly
 * related to the Blog.
 */
class BlogController extends Controller {

public function index(Request $request) {
        
	$blogResults = BlogModel::all();

	$currentPage = LengthAwarePaginator::resolveCurrentPage()-1;

	$collection = new Collection($blogResults);

	$perPage = 30;
	
	$currentPageBlogResults = $collection->slice($currentPage * $perPage, $perPage)->all();

	$paginatedBlogResults= new LengthAwarePaginator($currentPageBlogResults, count($collection), $perPage);

	return view('Blog::index', ['blogs' => $paginatedBlogResults]);
               
}
}

Above function call table from BlogModel and get all record from over there all record store in one object after that we find out current page using LengthAwarePaginator method which is resolveCurrentPage() with decrements by 1. It will give us current page. After that need to create a new laravel collection from the resulted data then define per page result which will display in page then from collection make slice to showing item in current page. for passing to view you have to create paginator using LengthAwarePaginator class where you need to pass current page blog result and total number of collection and pass per page number of record to show, finally return view with blog result and view file.

Now we will show record with pagination are as below code which will to write in to your index.balde.php file.

index.blade.php
// Call bootstrap css
@foreach($blogs as $blog)
                        
<p>{{ $blog->blog_name }}</p>
                           
@endforeach

<?php echo $blogs->setPath('blog')->render(); ?>

OR

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

In above code you can render blog result using setPath(‘pass your url prefix’) or directly render() whatever will require for your url pattern.