Here is what the table might look like
Store
- id
- modelable_id
- modelable_type
- name
- other columns
Shoe
- id
- sole_id (FK)
- other columns
Cloth
- id
- jean_id (FK)
- other columns
Bag
- id
- cover_id (FK)
- other columns
The model relationships for the parent model and related models
class Store extends Model
{
/**
* Get the store <Cloth|Bag|Shoe>
*/
public function modelable()
{
return $this->morphTo(__FUNCTION__, 'modelable_type', 'modelable_id');
}
}
class Shoe extends Model
{
/**
* Get the store that owns the shoe.
*/
public function store()
{
return $this->morphOne(Store::class, 'modelable');
}
}
class Cloth extends Model
{
/**
* Get the store that owns the cloth.
*/
public function store()
{
return $this->morphOne(Store::class, 'modelable');
}
}
class Bag extends Model
{
/**
* Get the store that owns the bag.
*/
public function store()
{
return $this->morphOne(Store::class, 'modelable');
}
}
When a store model is retrieved i want it to use
sole_id in Shoe instead of id
jean_id in Cloth instead of id
cover_id in Bag instead of id
by default laravel expects the foreign keys to be named the same thing
morph<One|To|Many>()methods accept multiple arguments that allows you to override these columns if they differ from standard naming conventions. Since you didn't include any actual code in your question (i.e. your methods and usage of these relationships), there isn't much else we can help you with. Please edit your question and include more details if you want more/better help.