Search code examples
laravel-5modelmultilingual

Laravel 5 bilingual Product model


I am looking for the most straightaway solution and breaking my head about implementing a bilingual Product model with only one basic requirements: the product query should only deliver results where the product name in the app()->locale language is set.

I'm stuck right at the beginning to decide wether I should keep completely different models (Product_en and Product_es), this would make querying easiest I guess, or have just one Product model with the English texts, with hasOne() methods pointing to the Spanish translations? In the latter case, how would I effectively query for entries which have translations?

Thanks a lot for any hints. Cheers.


Solution

  • I would create a language property for the Product model and would add a Scope for this, where you can filter the results with the value of App::getLocale().

    This way, any time you just query the product, you get the Product models on the actually selected language.