This package enables you to easily fetch localized columns from your models.
- You must have a table
languages
in your database - You must have a model for that table called
Language
-
Add
"mabasic/translate-that-model": "1.0.*"
to your composer require section and docomposer update
after that. -
Next, add
Mabasic\TranslateThatModel\TranslateThatModelServiceProvider
to your service_providers array inapp.config
. -
Publish config
php artisan config:publish Mabasic/TranslateThatModel
and edit config file with your model extension for translated models and language table column name for storing code (en, hr, it...). -
Add
use Mabasic\TranslateThatModel\TranslateThatModelTrait;
in your models that require translation.
// get realestates and eager load city and nested relationship translations
$realestate = Realestate::with('city.translations')->get();
// display translated name on current application locale
{{ $realestate->city->translations->first()->name }}
//or
// specify on which locale you want it returned
{{ $realestate->city->translations('hr')->first()->name }}
If you have any questions or need more usage examples or features feel free to open a issue or contact me.
This package is inspired by laravel-translatable.
The reason why I could not just use the package above is that it assumes that I keep locale field in every table that holds translations. I keep locale field in my language table. After looking at the source code I concluded that It would be simpler to create my own package, because I don't need those fancy model update options.