【问题标题】:Laravel Pagination when using a MySQL Stored Procedure使用 MySQL 存储过程时的 Laravel 分页
【发布时间】:2016-03-25 22:07:21
【问题描述】:

这可能是一个老问题,但似乎我无法解决这个问题。我有一个 Laravel 5.0 API,它调用一个从多个表中检索数据的过程。检索到大约 50-80 行,我想对服务结果进行分页。

这是代码:

$infoList = DB::connection('mysql')
    ->select(DB::raw('CALL dbname.GetAllUserInfo()'));

有没有办法对结果进行分页?即使我将数组转换为类并使用->paginate(15),它也会给我一个错误,即Call to undefined method stdClass::paginate()。我尝试使用 foreach 创建对象,但仍然无法使用分页。请问有什么建议吗?我是 Laravel 的初学者。

【问题讨论】:

    标签: php api pagination laravel-5


    【解决方案1】:

    如果你使用的是 laravel.5 那么你必须这样做分页。

    $infoList = DB::connection('mysql')
          ->select(DB::raw('CALL dbname.GetAllUserInfo()'));
    
    $page = Input::get('page', 1);
    $paginate = 10;
    
    $offSet = ($page * $paginate) - $paginate;
    $itemsForCurrentPage = array_slice($data, $offSet, $paginate, true);
    $data = new \Illuminate\Pagination\LengthAwarePaginator($itemsForCurrentPage, count($data), $paginate, $page);
    
    return view('test',compact('data'));
    

    【讨论】:

      【解决方案2】:

      你不能按照你想要的方式去做。因为 $infoList->paginate(..) 方法是在 Eloquent 模型/构建器中构建的,并且您拥有 Illuminate\Database\Query\Builder 实例。

      DB查看外观。

      对此有多种解决方案。您可以将$infoList 数组放在一个集合中并使用此slice 方法。

      $infoList = DB::connection('mysql')
          ->select(DB::raw('CALL dbname.GetAllUserInfo()'));
      
      $list = collect($infoList); // or new Collection($list);
      // paginate/slice the array into the set you want
      // first parameter: offset, second parameter the amount/limit
      $pageList = $list->slice($request->get('offset', 0), 30);
      

      另一种解决方案是创建一个模型并且不使用存储过程(但我猜这不是您需要的解决方案)。

      class User extends Illuminate\Database\Eloquent\Model
      {
          // model implementation
      }
      
      $infoList = User::paginate(30); // parameter: limit/size
      

      另一种解决方案是也使用存储过程并在存储过程调用中进行分页:

      // stored procedure
      DELIMITER //
      CREATE PROCEDURE GetAllUserInfo (
          IN _limit smallint unsigned, 
          IN _offset smallint unsigned
      )
      BEGIN
          SELECT Name, HeadOfState FROM Country
          WHERE Continent = con
          LIMIT _offset, _limit;
      END //
      DELIMITER ;
      
      // execute the stored procedure
      $infoList = DB::connection('mysql')
          ->select(DB::raw('CALL dbname.GetAllUserInfo('. $request->get('offset', 0) .', 30)'));
      

      注意:我写了一个存储过程已经有一段时间了,所以我写它的形状不太好。我把它写在我头顶的atm上。

      我希望这些解决方案之一对您有用。

      【讨论】:

        【解决方案3】:

        我刚刚改变了这个视图(对于 Laravel 5.1)

        {{ $products->links() }}
        

        为此:

        {!! str_replace('/?', '?', $storecreditlist->appends([])->render()) !!}
        

        我在这里也有类似的答案:https://www.onlineinterviewquestions.com/blog/laravel-pagination-with-array-or-object

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-07-22
          • 2017-05-05
          • 1970-01-01
          • 2013-06-29
          • 1970-01-01
          • 2016-05-08
          • 1970-01-01
          • 2013-01-19
          相关资源
          最近更新 更多