I faced some problem which I cannot resolve using Laravel's Eloquent.
I have two or more (6 - exactly) objects: - ObjectA - ObjectB - ObjectC Each of these objects has a one own Variant, but a variant structure is the same for each Object, so can be common. Variant has a filed variant_type which indicates for which type of Object belongs to. How to base relation on this field?
class Variant extends Model
{
protected $fillable = ['variant_id','name','variant_type','variant_img'];
public function ObjectsA()
{
return $this->belongsToMany(ObjectA::class);
}
public function ObjectsB()
{
return $this->belongsToMany(ObjectB::class);
}
.
.
.
}
In Objects models I have relation hasOne for Variant::class. I was thinking about creating pivot tables for each Object example: objectA_variant, objectB_variant etc... but it's not the point because Object has one variant. Also, I was thinking about creating nullable columns for foreign keys (6 columns), but I am not sure it's a good way?
Please advice and thanks in advance.