This is a Laravel package containing a trait for translatable Eloquent models. This package follows the approach to have only a single table to maintain all the translations.
said/laravel-translatable is a Laravel package for this is a laravel package containing a trait for translatable eloquent models. this package follows the approach to have only a single table to maintain all the translations..
It currently has 2 GitHub stars and 77 downloads on Packagist (latest version 0.03).
Install it with composer require said/laravel-translatable.
Discover more Laravel packages by said
or browse all Laravel packages to compare alternatives.
Last updated
This is a Laravel package containing a trait for translatable Eloquent models. This package follows the approach to have only a single table to maintain all the translations.
This approach may not be perfect for every use case as the table can grow really big. But compared to all the other packages this approach is the most flexible as it lets you make models and its attributes translatable without extra configuration.
Alternatives to this package are following packages:
This package require a minimum Laravel version of 5.8 and PHP in version 7.1
You can install the package via composer:
composer require said/laravel-translatable:0.0.7
Now you can use this Trait on any Eloquent Model of your project.
To make your Eloquent Model translatable just add the Said\Translatable\Traits\TranslatableTrait to your model. Then add a public attribute$translatable_columns` as an array containing all the attributes that should be translatable.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Said\Translatable\Traits\Translatable;
class MyModel extends Model
{
use Translatable;
protected $translatable_columns=[
'name'
];
protected $fillable=[
'price'
];
}
<?php
public function store(Request $request) {
$data = [
'en' => [
'name' => en-name
],
'fr' => [
'name' => fr-name
],
];
// Now just pass this array to regular Eloquent function
MyModel::create($data);
}
The simplest version of getting an translation is to simply get the property. This will return the value of the property in the current language
// assuming $myModel is an instace of MyModel class defined above
// and the translations are set
echo $myModel->name; // returns 'Product'
App::setLocale('fr');
echo $myModel->name; // returns 'Produit'
You can also use
$myModel->in('fr')->translate('name'); // returns 'Produit'
soon
Check CHANGELOG for the changelog
soon
soon
soon
soon.
The MIT License (MIT).