Laravel manual pagination with array example

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

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:

Example:

public function myData($userid)
{
    $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;
}

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.

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 →