dimanche 12 avril 2020

How to maintain each product variant stock in Laravel ecommerce

I'm using these tables in my ecommerce application:

Categories:

id | name | parent_id | slug

Products:

id | sku | name | description | slug | weight 

Product_categories:

id | category_id | product_id 

attributes:

id | code | name 

Note: Attributes table will store color and size

attributes_values:

id | attribute_id | value

Note: Attribute values will store the value of color and sizes, here color may be red, and size be Small,Large

Product_attributes:

id | attribute_id | value | qty | price | product_id

I want to maintain stock of every variant, for now, only variant of product is its size, color, i want to show user on home page and Product detail page, if a particular product variant is out of stock then make that variant disabled.

These are some of the relation that i'm using:

Product Model:

public function attributes()
{
    return $this->hasMany(ProductAttribute::class);
}
public function categories()
{
    return $this->belongsToMany(Category::class, 'product_categories', 'product_id', 'category_id');
}
public function product_attributes()
{
    return  $this->belongsToMany(Attribute::class, 'product_attributes')->withPivot('value', 'quantity','price');
}

ProductAttribute:

public function product()
    {
        return $this->belongsTo(Product::class);
    }

public function attributesValues()
{
    return $this->belongsToMany(AttributeValue::class);
}

public function attribute()
{
    return $this->belongsTo(Attribute::class);
}

AttributeValue Model:

public function attribute()
    {
        return $this->belongsTo(Attribute::class);
    }

public function productAttributes()
{
    return $this->belongsToMany(ProductAttribute::class);
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire