Reputation: 765
I am trying to retrieve all my products of the current and child categories recursively, but unfortunatly it always returns an empty array. Here is my method.
class Category {
public static $products = [];
public static function products($id) {
array_merge(self::$products, DB::table('products')->where('category_id', $id)->get());
$categories = DB::table('categories')->where('parent_id', $id)->get();
foreach ($categories as $category) {
self::products($category->id);
}
return self::$products;
}
}
After executing this method Category::products is an empty array. Can someone help me find the correct way to do this?
Upvotes: 1
Views: 51
Reputation: 781761
You need to assign the result of array_merge
. It returns a new array, it doesn't modify the argument array.
self::$products = array_merge(self::$products, DB::table('products')->where('category_id', $id)->get());
Upvotes: 1