A small plugin for managing multi-language in database table.
Require this package with composer using the following command:
composer require riseno/localizable
After updating composer, add the service provider to the providers
array in config/app.php
Riseno\Localizable\LocalizableServiceProvider::class,
Run artisan command to generate the database migration file
php artisan riseno:localizable:generate user
After generated migration file, open the migration file. Add any column that should be localized.
Schema::create('user_localizations', function(Blueprint $table)
{
$table->increments('id');
$table->unsignedInteger('user_id');
$table->string('locale');
$table->boolean('default')->default(false);
$table->string('name')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
Once all the columns is added to migration file, run migrate command
php artisan migrate
Go to the model that you want to implement localizable function, and add this trait to the class
use Riseno\Localizable\LocalizableTrait;
class User extends Authenticatable
{
use LocalizableTrait;
And also two required properties
protected $localizeModel = UserLocalizations::class;
protected $localizeFields = ['name'];
One more thing, add a localizations method
public function localizations()
{
return $this->hasMany(UserLocalizations::class, 'user_id', 'id');
}
The trait has provided a generic method for accessing the localized data,
$user->localize('en_US');
// or access value directly
$user->localize('en_US')->name;
If you want to save / update the localized record
$user->saveLocalize('en_US', ['name' => 'Riseno']);
The Laravel Localizable is open-sourced software licensed under the MIT license