Hi i have two tables CATEGORY and PRODUCT i need SUBCATEGORY and then PRODUCT
this is my database migration
Schema::create('categories', function($table){ $table->increments('id'); $table->string('name'); $table->timestamps(); });
Schema::create('products', function($table){ $table->increments('id'); $table->integer('category_id')->unsigned(); $table->foreign('category_id')->references('id')->on('categories'); $table->string('title'); $table->text('description'); $table->decimal('price', 6, 2); $table->boolean('availability')->default(1); $table->string('image'); $table->timestamps(); });
You need an extra column in your category table, that will hold the parent category's id. Such that for all your top categories this will have 0, then all the child categories have the parent category value. See e.g. below
id name parent_id
1 pet 0
2 cat 1
3 dog 1
4 vehicle 0
5 sedan 4
6 truck 4
Hope this helps
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community