I am working on an eCommerce application.
I have 3 models. Product, Color, ProductImage.
The relation between Product and Color is: belongsToMany
<?php
namespace App\Entities\Product;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
/**
* A product has many colors.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function colors()
{
return $this->belongsToMany(Color::class);
}
}
The relation between Color and ProductImage is: hasMany
<?php
namespace App\Entities\Product;
use Illuminate\Database\Eloquent\Model;
class Color extends Model
{
/**
* A color has many images.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function images()
{
return $this->hasMany(ProductImage::class);
}
}
Question is how to make a relation between the
ProductandProductImagemodel?How to design the
product_imagestable?
id, color_id, image
like this?
my use case is, I will make a slider on a single product page. There will be a Color select option. If a user selects a color, need to load product images associate with that selected color.
Appreciate your help :)

