Back

How do I manually create a Paginator in 5


Hi I've looking round for a while now but cant figure this out how do How do I manually create a Paginator instance in version 5 I used to be able to do $paginator = Paginator::make($items, $totalItems, $perPage); but this no longer works any help really appreciated

jimgwhit replied 3 years ago

Type in search box laravel 5 pagination, There's a post with about 14 responces that will walk you through.
The post is so good a 5 year old clild could figure it out. Please search first. In fact here you go:
http://laravel.io/forum/11-13-2014-laravel-5-pagination

Ardakilic replied 3 years ago

Try this:

Route::get('hede', function(){
    $paginator = new Illuminate\Pagination\LengthAwarePaginator(
        range(1,500), //a fake range of total items, you can use range(1, count($collection))
        500, //count as in 1st parameter
        20, //items per page
        \Illuminate\Pagination\Paginator::resolveCurrentPage(), //resolve the path
        ['path' => \Illuminate\Pagination\Paginator::resolveCurrentPath()]
    );

    return $paginator->render();
});

Works great for me so far.

shez1983 replied 3 years ago

yeah except he asked for it 8 months ago..

Rajiv replied 6 months ago

Its very easy to add pagination in laravel

  1. Paginating Database Results
$users = DB::table('users')->paginate(15);
  1. View 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

For more follow this post.

How To Add Pagination In Laravel

Pardeeptech replied 5 months ago
 $data = static::get();
    

    $result = [];
    if(!empty($data)){
        foreach ($data as $key => $value) {
            $result[$value->type.'-'.$value->postid][] = $value;
        }
    }
    

    $paginate = 10;
    $page = Input::get('page', 1);
    

    $offSet = ($page * $paginate) - $paginate;  
    $itemsForCurrentPage = array_slice($result, $offSet, $paginate, true);  
    $result = new \Illuminate\Pagination\LengthAwarePaginator($itemsForCurrentPage, count($result), $paginate, $page);
    $result = $result->toArray();
    return $result;
Harikrishna Kundariya replied 5 months ago

You need to create an instance of either an Illuminate\Pagination\Paginator or Illuminate\Pagination\LengthAwarePaginator.

Hope this article helpful to you for creating a Paginator manually: http://laravel.com/docs/master/pagination

I think it'll look something like this:

Use Illuminate\Pagination\Paginator

class Xyz {
    public function index()
    {
        // Build array
        $array = [];
        return new Paginator($array, $perPage);;
    }
}
Pardeeptech replied 4 months ago
$data = static::get();
    

    $result = [];
    if(!empty($data)){
        foreach ($data as $key => $value) {
            $result[$value->type.'-'.$value->postid][] = $value;
        }
    }
    

    $paginate = 10;
    $page = Input::get('page', 1);
    

    $offSet = ($page * $paginate) - $paginate;  
    $itemsForCurrentPage = array_slice($result, $offSet, $paginate, true);  
    $result = new \Illuminate\Pagination\LengthAwarePaginator($itemsForCurrentPage, count($result), $paginate, $page);
    $result = $result->toArray();
    return $result;

2nd method Create pagination by using Vue with Laravel


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us