laravel从数组取数据实现分页功能

21. October 2017 Laravel 0

public function redisrest(Request $request)
{
$users = Redis::hgetall(Auth::id());
// var_dump($data);
$perPage = 3;
$currentPage = 1;
if ($request->has(‘page’)) {
$current_page = $request->input(‘page’);
$current_page = $current_page <= 0 ? 1 : $current_page;
} else {
$current_page = 1;
}
$item = array_slice($users, ($current_page – 1) * $perPage, $perPage); //注释1
$total = count($users);
$paginator = new LengthAwarePaginator($item, $total, $perPage, $currentPage, [
‘path’ => Paginator::resolveCurrentPath(), //注释2
‘pageName’ => ‘page’,
]);
$userlist = $paginator->toArray()[‘data’];
$result = compact(‘userlist’, ‘paginator’);
return view(‘test’, [‘result’ => $result]);

}