A
A
Alex Me2016-12-07 19:46:50
Laravel
Alex Me, 2016-12-07 19:46:50

How to work with relationships in Laravel without secondary models?

Hello!
The relationship docs sort of clearly reveal everything - we have a User model, $user can have a phone, which means we are making a relationship between the User and Phone models. What if I don't want to create a separate model for Phone, but still want to get advanced phone data from another table?
I have a situation where I have a certain Metadata table (the Metadata model is based on it). And there is a table with translations for each element from Metadata (MetadataTranslations). I don’t see a separate logical reason for creating a translation model for Metadata - translations exist only under Metadata (MetadataTranslation will never be reassigned to another Metadata model, as it could be with Phone and User or Comment and Post). Accordingly, I would like all operations with Metadata and translations to be carried out within the Metadata model.
I've been getting to grips with Laravel not so long ago, but I can't figure out how to properly approach the architecture for a bit. If possible, explain how you would have done in my situation, which I described above. Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Faim, 2016-12-07
@iamjack

There is an excellent package dimsav/laravel-translatable to implement translation .
If this is a one-to-one relationship, then you can get by with an ordinary join. In other cases, it is more convenient to use Eloquent relationships.

Вячеслав Плиско, 2016-12-08
@AmdY

Вы хотите архитектуру или костыли? Есть MetadataTranslations, создавайте для неё модель. Экономя на спичках вы ничего не выиграете.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question