简体   繁体   English

Laravel 5.4-从数组集合产品分页

[英]Laravel 5.4 - Paginate from array collection products

i'm trying to paginate a collection's products, like this: 我正在尝试对集合的产品进行分页,如下所示:

$products_array = array();

        for ($i=0; $i < count($t); $i++) { 

            $p = Product::where('category_id',$t[$i]->category_default)->where('project_id',$t[$i]->id)->first();
            array_push($products_array, $p);
        }

        $paginate = new Illuminate\Pagination\LengthAwarePaginator($products_array, count($products_array), 10, 1, ['path'=>url('api/products')]);

        dd($paginate);

But i get this error: 但是我得到这个错误:

FatalThrowableError in HomeController.php line 75: Class 'Shop\\Http\\Controllers\\Illuminate\\Pagination\\LengthAwarePaginator' not found 在HomeController.php第75行中出现FatalThrowableError:找不到类“ Shop \\ Http \\ Controllers \\ Illuminate \\ Pagination \\ LengthAwarePaginator”

i don't know if this is the best way to do this, I would appreciate your help. 我不知道这是否是执行此操作的最佳方法,不胜感激。

The error is really self explaining. 错误实际上是在自我解释。 You are trying to use Illuminate\\Pagination\\LengthAwarePaginator from current namespace and this is Shop\\Http\\Controllers . 您正在尝试从当前名称空间使用Illuminate\\Pagination\\LengthAwarePaginator ,这是Shop\\Http\\Controllers

To fix this, either add leading backslash like so: 要解决此问题,请添加前导反斜杠,如下所示:

$paginate = new \Illuminate\Pagination\LengthAwarePaginator($products_array, count($products_array), 10, 1, ['path'=>url('api/products')]);

or at the beginning of this PHP file (after opening PHP tag) add new line: 或在此PHP文件的开头(打开PHP标记后)添加新行:

use Illuminate\Pagination\LengthAwarePaginator;

and then you can write above line like so: 然后您可以像上面这样写:

$paginate = new LengthAwarePaginator($products_array, count($products_array), 10, 1, ['path'=>url('api/products')]);

To read more about namespace in PHP you can look here: https://stackoverflow.com/a/24607087/3593996 要了解有关PHP中命名空间的更多信息,请参见: https : //stackoverflow.com/a/24607087/3593996

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM