Laravel 5 manual pagination with array example

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

If you neeed to create custom pagination of your array, you can learn from this post. basically we are doing pagination with model or DB facade like "User::paginate(10)" OR "DB::table('users')->paginate(10)". But now if you have new array and you want to make pagination on your array then you can also give using "LengthAwarePaginator" class see following lesson:

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;

}

Recent Posts

Related Posts