Home Laravel Laravel manual pagination with array example

Laravel manual pagination with array example

by Shahriar Sagor

Today now in this post i will show you Laravel manual pagination with array. If we need to create a custom pagination of our array, we can also learn from this post. It is basically we are doing by pagination with model or DB facade like “User::paginate(10)” OR “DB::table(‘users’)->paginate(10)”. But now if we need a new array and we want to make it pagination on our array then we can also give by using “LengthAwarePaginator” class see following example:


public function myData($userid)
    $data = static::get();

    $result = [];
        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;

Read Also : How to Create Custom Blade Directive in Laravel?

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


You may also like