1

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?

1
  • you never add any products to the Categoriy::products array... Commented Jul 31, 2015 at 12:07

1 Answer 1

1

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());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.