Hadrien's avatar
Level 24

Attributes for products

Hello,

I'm creating a small website for myself to show some digital products. Each product belongs to a category, and a category has many products. Every category has specific attributes.

For example, a HTML website may be retina ready, responsive… A WordPress website may be retina and responsive too, but also support some plugins.

I have created a migration, but I'm not sure it is best optimized, so I would like to get your opinion and advices.

I am sure it would help other people, because it is very common to create attributes for a eCommerce, portfolio or anything else.

        Schema::create('attributes', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('attribute_type_id')->unsigned()->index();
            $table->string('photo')->nullable();
            $table->boolean('visible')->default(true);
            $table->integer('position')->unsigned()->default(1);
        });

        Schema::create('attribute_type', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->boolean('visible')->default(true);
            $table->integer('position')->unsigned()->default(1);
        });

        Schema::create('attribute_category', function (Blueprint $table) {
            $table->integer('attribute_id')->unsigned()->index();
            $table->foreign('attribute_id')->references('id')->on('attributes')->onDelete('cascade');
            $table->integer('category_id')->unsigned()->index();
            $table->foreign('category_id')->references('id')->on('attributes')->onDelete('cascade');
        });
0 likes
1 reply
mcenroe's avatar

If I understand you well, what you have to do then is making a Many to Many relationship between a Category and an Attribute, being for example "Wordpress Site" the main category and "responsive", "retina", etc the attributes. That way you'll be able to query attributes and then filter them by their category if needed. Then you need a One To Many relationship between the Products and Categories. I don't understand why in your example you added an attribute_type table, with the attributes table only should be enough.

Hope this helps.

Please or to participate in this conversation.