Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组laravel的分页_Php_Laravel - Fatal编程技术网

Php 数组laravel的分页

Php 数组laravel的分页,php,laravel,Php,Laravel,我正在使用Laravel,我尝试对我的产品使用分页 array:4 [▼ 0 => Product {#770 ▶} 1 => Product {#766 ▶} 2 => Product {#814 ▶} 3 => Product {#846 ▶} ] 我用这样的方法得到这些 $this->getAllCategoriesProducts(31) 31是类别的ID 这是我的方法: public function getAllCategorie

我正在使用Laravel,我尝试对我的产品使用分页

array:4 [▼
  0 => Product {#770 ▶}
  1 => Product {#766 ▶}
  2 => Product {#814 ▶}
  3 => Product {#846 ▶}
]
我用这样的方法得到这些

$this->getAllCategoriesProducts(31)
31是类别的ID

这是我的方法:

  public function getAllCategoriesProducts($cat_id){
      $allProducts = [];
      foreach(ProductCategory::find($cat_id)->children()->get() as $subCategory){
        if($subCategory->children()->exists()){
          foreach($subCategory->children()->get() as $subSubCategory){
            foreach($subSubCategory->products()->get() as $product){
            $allProducts[] = $product;
            }
          }
          if($subSubCategory->children()->exists()){
            foreach($subSubCategory->children()->get() as $subSubSubCategory){
              foreach($subSubSubCategory->products()->get() as $product){
              $allProducts[] = $product;
              }
              if($subSubSubCategory->children()->exists()){
                foreach($subSubSubCategory->children()->get() as $subSubSubSubCategory){
                  foreach($subSubSubSubCategory->products()->get() as $product){
                  $allProducts[] = $product;
                  }
                }
              }
            }
          }
        }
      }
      return $allProducts;
    }
但是当我使用

$this->getAllCategoriesProducts($categroyId)->paginate(8)
它给了我这个错误

对数组上的成员函数paginate()的调用


您可以像这样手动添加分页器

use Illuminate\Pagination\LengthAwarePaginator;

$products = $this->getAllCategoriesProducts(31);
$total = count($products);
$perPage = 5; // How many items do you want to display.
$currentPage = 1; // The index page.
$paginator = new LengthAwarePaginator($products, $total, $perPage, $currentPage);

您可以像这样手动添加分页器

use Illuminate\Pagination\LengthAwarePaginator;

$products = $this->getAllCategoriesProducts(31);
$total = count($products);
$perPage = 5; // How many items do you want to display.
$currentPage = 1; // The index page.
$paginator = new LengthAwarePaginator($products, $total, $perPage, $currentPage);

Laravel的paginator与查询生成器和雄辩的ORM集成在一起。如果您仍然每页获得整个结果集,则检查文档分页的价值是否较低。如果您能更好地描述您想要从哪些表中获取哪些数据,我们可以更好地帮助您。@online Thomas我有多级分类,因此通过上述方法,我得到了所有相关的分类产品,我想对它们进行分页,只是Thislavel的分页器与查询生成器和雄辩的ORM集成在一起。如果您仍然每页获得整个结果集,则检查文档分页的价值是否较低。如果您能更好地描述您想要从哪些表中获取哪些数据,我们可以更好地帮助您。@online Thomas我有多级分类,因此使用上述方法,我可以获得所有相关的分类产品,并且我想在下面对它们进行分页