【问题标题】:How to set pagination in laravel 5 while passing an array from the controller从控制器传递数组时如何在 laravel 5 中设置分页
【发布时间】:2019-07-14 22:09:10
【问题描述】:

这是我的代码

控制器

$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 和 post 操作时返回几个结果集

查看

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

<?php
}
?>

我尝试了几种方法来设置分页方法..但它对我不起作用..

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

提前致谢

【问题讨论】:

    标签: php laravel laravel-5 pagination


    【解决方案1】:

    您可以使用手动创建分页器

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

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

    【讨论】:

      【解决方案2】:

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

      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]);
      }
      

      【讨论】:

        【解决方案3】:
        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 分页或使用自定义代码在视图上添加分页。

        【讨论】:

        • 在控制器中使用此代码并添加 ` echo $aHotelRooms->render(); ` in view 可以设置分页!!...
        • 在我的控制器函数中我已经传递了一个Request $request 我应该如何使用,$page=1 !!...
        • 当使用echo $Rooms-&gt;render() 时,我总是得到1 FatalErrorException in 3d4c6f287876462533e51d881f81767c line 45: Call to a member function render() on array 我应该怎么做.. : (
        猜你喜欢
        • 2018-07-31
        • 1970-01-01
        • 1970-01-01
        • 2015-12-27
        • 1970-01-01
        • 2019-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多