Skip to content
Advertisement

Laravel: Multilanguage details of a model

I have a model named Point having the following fields:

  • name
  • description
  • lat
  • lng

The fields “name” and “description” can be in several languages, so I created two tables for points and their details.

JavaScript

There is an index unique on point_id/language.

In the model files I have One To Many relationships

JavaScript

Now I want to get the Point with details based on User language. I do so in the PointController:

JavaScript

Can I avoid the “with” clause in the Controller? Maybe making the right query in the Point model file. I’m looking for a way to return the point with one detail associated with it, based on language of the Auth::user().

Thanks for any suggestion.

Advertisement

Answer

you can add query to relationship method in point class to details method like that :

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement