I'm dealing with a challenge where I want to create a polymorphic model Localization. This will include long-text columns like default, en_us, de_de,...
Now imagine another model Product. Products usually have text attributes like name, description,... Here comes the part, where I could use a polymorphic relation with Localizations. The localizations table is supposed to have this type of structure:
id | localizable_id | localizable_type | default | en_us | de_de |
---|---|---|---|---|---|
1 | 25 | App\Model\Product | Phone | null | Telefon |
2 | 25 | App\Model\Product | The best phone on the market without an audio jack | null | Das beste Telefon auf dem Markt ohne Audioanschluss |
3 | 15 | App\Model\Article | Top 10 products | null | Top 10 Produkte |
4 | 15 | App\Model\Job | Salesman | null | Verkäufer |
If I want a Product to have name and description localizable, then according to the Laravel documentation, you should expect something like this in my code:
class Product extends Model
{
public function name() // Expecting model with default attribute 'Phone'
{
return $this->morphOne(Localization::class,'localizable');
}
public function description() // Expecting model with default attribute 'The best phone on the market without an audio jack'
{
return $this->morphOne(Localization::class,'localizable');
}
}
No matter how obvious it is, it won't work correctly, because I can't expect two identical methods to return different values.
On the other side, if I wanted to follow Laravel's convention, the Localization model is supposed to look like this:
class Localizable extends Model
{
public function localizable()
{
return $this->morphTo(__FUNCTION__,'localizable_type','localizable_id');
}
}
As you can see in the example above, the polymorphic relation is inverse to what I need. The problem is that I want one table with all the strings, which might be translated (localizables) and use them in many other models than just the Product, like Shop, Job, Article,...
I need to somehow implement the distinction not only between localizable_types but also on which column/attribute it is supposed to be related. What is the best approach to achieve this?