Sunday, April 16, 2017

How to show result as nested categories in Laravel 5.2

Intoduction:
Often the programmer wants to build nested product categories so that he can customize the type of products he has and also for easy modification it.

Idea:
first: i have 5 migrations about categories tables

// migration: 1
Schema::create('products_categories_1', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
});

// migration: 2
Schema::create('products_categories_2', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('related_id')->unsigned();
    $table->string('name');
    .....
});

// migration 3, 4 and 5 is the same with 2

migrations view example taken from database
products_categories_1 table products_categories_2 table

To be aware, I have completed the system of adding, modifying and deleting any of these categories.

second: i have products migration

Schema::create('products', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('category_table_number');
    $table->integer('category_id');
    ..... 
});

products table

by applying this code

Product::users_roles()
    ->where('category_table_number', $category_table_number)
    ->where('category_id', $category_id)
    ->lists('id');

Issue:
With this previous code, the product with the direct rating is obtained only, but I want to get all products with nested categories

example:

Product::users_roles()
    ->where('category_table_number', 1)
    ->where('category_id', 4)
    ->lists('id');

// Result: array('11') (from product table image)

product table products_categories_1 table

// but i want to appear all products with nested categories like
// Result: array('11', '1') (from product table image)

products table produts_categories_2 table

I wish:
I could find a solution to that problem or if I had a better idea of making overlapping classifications, please see me it


Thanks for any help



via Ahmed Sk

Advertisement