Reputation: 189
I'm using Laravel and I try to use pagination for my products
array:4 [▼
0 => Product {#770 ▶}
1 => Product {#766 ▶}
2 => Product {#814 ▶}
3 => Product {#846 ▶}
]
And I get these from a method like this
$this->getAllCategoriesProducts(31)
31 is ID of category
and this is my method :
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;
}
but when I use
$this->getAllCategoriesProducts($categroyId)->paginate(8)
it gives me this error
Call to a member function paginate() on array
Upvotes: 1
Views: 5776
Reputation: 518
You can add paginator manually like this.
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);
Upvotes: 7