Recursive php method getting products

At this point, I am trying to recursively return all products from the children of the specified category.

I am using laravel and create a class to handle this in App / Helpers so that it is automatically loaded.

Database structure Categories:

1|0|2015-07-28 14:45:38|2015-07-28 14:45:38
2|1|2015-07-28 14:52:43|2015-07-28 14:56:47

      

Products:

1|2|2015-07-28 15:10:45|2015-07-29 11:04:44

      

So the product has a Category ID 2. Category 2 has a Parent ID of 1. So if a user is on a Category 1 page, he / she should see products that are Category 1 and Category 2, because 2 is a Category 1 child.

This is my current method:

 public static function getProducts($id) {

        self::$products =  DB::table('products')->where('category_id', $id )->get();

        $categories = DB::table('categories')->where('parent_id', $id)->get();
        if ($categories !== null) {
            foreach ($categories as $category) {
                array_merge(self::$products, self::getProducts($category->id) );
            }
        } 
        return self::$products;
    }

      

I hope someone can help me to return recursively all the products belonging to child categories.

Update:

I installed the Laravel Debug Toolbar and found out that my queries don't return any result when the web page is loaded. When I query it using my SQL client, I get the correct result.

This is my current method for getting all products from subcategories:

public static function getProducts($id)
    {

        $products = [];
        $categories = DB::table('categories')->where('parent_id', $id)->get();
        array_merge($products, DB::table('products')->where('category_id', $id)->get());
        if ($categories !== null) {
            foreach ($categories as $category) {

                self::getProducts($category->id);
            }
            return $products;
        }
    }

      

Database schema:

CREATE TABLE "products" ("id" integer not null primary key autoincrement, "url" text not null, "title" text not null, "keywords" text not null, "description" text not null, "content" text not null, "attributes" text not null, "pdf" text not null, "image" text not null, "category_id" integer not null, "created_at" datetime not null, "updated_at" datetime not null);
CREATE TABLE "categories" ("id" integer not null primary key autoincrement, "url" text not null, "title" text not null, "keywords" text not null, "description" text not null, "content" text not null, "parent_id" integer not null, "created_at" datetime not null, "updated_at" datetime not null);

      

Also opened debug panel and site => http://remotefix.nl:81/producten/flenzen

If anyone can help me it would be appreciated.

+3


source to share


1 answer


In Laravel,

hasMany () and belongs to To () creates a parent / child category relationship.

Just use in a model that expands brightness. For a sample,

    public function getChildValues() {
        return $this->hasMany('Category', 'parentid');
    }     

    public function getParentValues() {
        return $this->belongsTo('CategoryMN', 'id');
    } 

      



This link can help you.

And to extract, for example, in the Controller,

$categories_child = Category::with('getChildValues')->get(); 

      

+3


source







All Articles