Introduction
Laravel Translatable
If you want to store translations of your models into the database, this package is for you.
This is a Laravel package for translatable models. Its goal is to remove the complexity in retrieving and storing multilingual model instances. With this package you write less code, as the translations are being fetched/saved when you fetch/save your instance.

Installation

1
composer require astrotomic/laravel-translatable
Copied!

Quick Example

Getting translated attributes

1
$post = Post::first();
2
echo $post->translate('en')->title; // My first post
3
4
App::setLocale('en');
5
echo $post->title; // My first post
6
7
App::setLocale('de');
8
echo $post->title; // Mein erster Post
Copied!

Saving translated attributes

1
$post = Post::first();
2
echo $post->translate('en')->title; // My first post
3
4
$post->translate('en')->title = 'My cool post';
5
$post->save();
6
7
$post = Post::first();
8
echo $post->translate('en')->title; // My cool post
Copied!

Filling multiple translations

1
$data = [
2
'author' => 'Gummibeer',
3
'en' => ['title' => 'My first post'],
4
'fr' => ['title' => 'Mon premier post'],
5
];
6
$post = Post::create($data);
7
8
echo $post->translate('fr')->title; // Mon premier post
Copied!

Tutorials

Credits

Versions

Package
Laravel
PHP
v11.6 - v11.9
5.8.* / 6.* / 7.* / 8.*
>=7.2
v11.4 - v11.5
5.6.* / 5.7.* / 5.8.* / 6.*
>=7.1.3
v11.0 - v11.3
5.6.* / 5.7.* / 5.8.*
>=7.1.3

Treeware

You're free to use this package, but if it makes it to your production environment I would highly appreciate you buying the world a tree.
It’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to plant trees. If you contribute to my forest you’ll be creating employment for local families and restoring wildlife habitats.
You can buy trees at https://offset.earth/treeware
Read more about Treeware at https://treeware.earth
Last modified 11mo ago