astrotomic/laravel-translatable
Laravel package for translatable Eloquent models. Store model translations in the database and automatically fetch/save multilingual attributes based on locale, reducing boilerplate when working with multi-language content.
You can use custom rules to validate unique and exists rules for translatable attributes.
Ensure that the attribute value is unique by checking its absence in the database; if the value already exists, raise a validation exception.
use Astrotomic\Translatable\Validation\Rules\TranslatableUnique;
...
$person = new Person(['name' => 'john doe']);
$person->save();
$data = [
'name' => 'john doe',
'email' => 'john@example.com'
];
$validator = Validator::make($data, [
'name' => ['required', new TranslatableUnique(Person::class, 'name')],
]);
use Astrotomic\Translatable\Validation\Rules\TranslatableUnique;
...
$person = new Person(['name' => 'john doe']);
$person->save();
$data = [
'name:en' => 'john doe',
'email' => 'john@example.com'
];
$validator = Validator::make($data, [
'name:en' => ['required', Rule::translatableUnique(Person::class, 'name:en')],
]);
use Illuminate\Validation\Rule;
...
$person = new Person(['name' => 'john doe']);
$person->save();
$data = [
'name:en' => 'john doe',
'email' => 'john@example.com'
];
$validator = Validator::make($data, [
'name:en' => ['required', Rule::translatableUnique(Person::class, 'name:en')],
]);
Verify if the attribute value exists by confirming its presence in the database; if the value does not exist, raise a validation exception.
use Astrotomic\Translatable\Validation\Rules\TranslatableExists;
...
$person = new Person(['name' => 'john doe']);
$person->save();
$data = [
'name' => 'john doe',
'email' => 'john@example.com'
];
$validator = Validator::make($data, [
'name' => ['required', new TranslatableExists(Person::class, 'name')],
]);
use Illuminate\Validation\Rule;
...
$person = new Person(['name' => 'john doe']);
$person->save();
$data = [
'name:en' => 'john doe',
'email' => 'john@example.com'
];
$validator = Validator::make($data, [
'name:en' => ['required', Rule::translatableExists(Person::class, 'name:en')],
]);
How can I help you explore Laravel packages today?