我已经定义了三个级别的类别。主要、次要和产品类别。
我在primary_categories
表下有两个类别,即女装和男装。
在secondary_categories
我下面有传统服装(女性)、鞋类(女性)、西方(女性)、西方服装(男性)、鞋类(男性)、裤子(男性)等类别。
最后,product_categories
我有裤子、T 恤、库尔塔、凉鞋等类别。
在为产品保存类别时,我products
在列中使用了表category_id
。
现在我想获得属于女性时尚的产品。我怎样才能做到这一点?
主要类别
public function up()
{
Schema::create('primary_categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}
次要类别
public function up()
{
Schema::create('secondary_categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug');
$table->unsignedBigInteger('primary_category_id')->nullable();
$table->foreign('primary_category_id')->references('id')->on('primary_categories')->onDelete('SET NULL');
$table->timestamps();
});
}
最终类别
public function up()
{
Schema::create('product_categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug')->unique();
$table->unsignedBigInteger('secondary_category_id')->nullable();
$table->foreign('secondary_category_id')->references('id')->on('secondary_categories')->onDelete('SET NULL');
$table->timestamps();
});
}
在添加产品时,category_id
product_categories 进入产品表中。