As of now, there is no dedicated Laracasts series or episode specifically about using Spatie's Laravel Translatable package for Eloquent models. However, you can still implement it easily by following the official documentation: https://spatie.be/docs/laravel-translatable/v6/introduction
Here's a quick example of how to use Spatie Translatable in your Eloquent model:
- Install the package:
composer require spatie/laravel-translatable
- Add the
Translatabletrait to your model and define which attributes are translatable:
use Spatie\Translatable\HasTranslations;
class Post extends Model
{
use HasTranslations;
public $translatable = ['title', 'description'];
}
- Set and get translations:
$post = Post::find(1);
$post->setTranslation('title', 'en', 'My Post Title');
$post->setTranslation('title', 'fr', 'Mon Titre de Poste');
$post->save();
echo $post->getTranslation('title', 'fr'); // Mon Titre de Poste
echo $post->title; // returns translation for the app's current locale
While there's no Laracasts episode directly for this, videos in the "Eloquent Techniques" and "Advanced Eloquent" series may help you understand how to work with Eloquent models, which makes adopting this package easier.
If you want a full walk-through, the official documentation is the best starting point.