如何在从控制器传递数组时在laravel 5中设置分页

时间:2016-03-11 07:30:10

标签: php laravel laravel-5 pagination

这是我的代码

控制器

$query = "SELECT `h`.*,".$countquery.",".$minquery." FROM `abserve_hotels` as `h`";
$aReslts = $this->model->getData($query,$sqlCond);

$this->data['Rooms'] =!empty($aReslts) ? $aReslts : '';

return view('hotel.list', $this->data);

此处,$sqlCond在传递Ajax和后期操作

时返回多个结果集

查看

<?php
if(($Rooms)){
?>
@foreach($Rooms as $room)
{{ $room->id }}
@endforeach

<?php
}
?>

我尝试了几种设置分页方法的方法。但是它对我起作用了..

我应该如何为此做分页......有人可以帮我!! ..

提前致谢

3 个答案:

答案 0 :(得分:1)

您可以使用

手动创建分页器

$pagination = Paginator::make($this->data['Rooms'], count($this->data['Rooms']), 5);

要显示链接,请使用echo $pagination->links();{{ $pagination->links() }}

答案 1 :(得分:0)

public function controllerFunction($page=1)
{
   $limit = 10;
   $start = ($page-1)*$limit;
   $query = "SELECT `h`.*,".$countquery.",".$minquery." FROM `abserve_hotels` as `h`";
$aReslts = $this->model->getData($query,$sqlCond)->skip($start)->take($limit);

$this->data['Rooms'] =!empty($aReslts) ? $aReslts : '';

return view('hotel.list', $this->data);
}

这将为您提供分页内容。您可以通过laravel分页或使用自定义代码在视图中添加分页。

答案 2 :(得分:0)

使用这种方式....非常简单

public function items(Request $request)
{
    $items = [
        'item1',
        'item2',
        'item3',
        'item4',
        'item5',
        'item6',
        'item7',
        'item8',
        'item9',
        'item10'
        ];

    // Get current page form url e.x. &page=1
    $currentPage = LengthAwarePaginator::resolveCurrentPage();

    // Create a new Laravel collection from the array data
    $itemCollection = collect($items);

    // Define how many items we want to be visible in each page
    $perPage = 1;

    // Slice the collection to get the items to display in current page
    $currentPageItems = $itemCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->all();

    // Create our paginator and pass it to the view
    $paginatedItems= new LengthAwarePaginator($currentPageItems , count($itemCollection), $perPage);

    // set url path for generated links
    $paginatedItems->setPath($request->url());

    return view('items_view', ['items' => $paginatedItems]);
}