Saving the model

There are several ways to save the translation data into your model.

Set the translation data for each language

$newProduct = new Product([
    'name'      => 'Amazon Kindle Oasis',
    'published' => true,
]);

$newProduct->translateTo('en');
$newProduct->description = 'Product description in English';
$newProduct->details = 'Product details in English';

$newProduct->translateTo('es');
$newProduct->description = 'Product description in Spanish';
$newProduct->details = 'Product details in Spanish';

$newProduct->save();

// This way also works on updating a model

App::setLocale('es');
$product = Product::find(1);

// This will automatically set the translation for `es` language.
$product->description = 'Updated product description in Spanish';
$product->save();

Fill the translation data with mass assignment

$product = Product::create([
    'name'        => 'Amazon Kindle Oasis',
    'published'   => true,
    'description' => [
        'en' => 'Product description in English',
        'es' => 'Product description in Spanish',
    ],
    'details'     => [
        'en' => 'Product details in English',
        'es' => 'Product details in Spanish',
    ],
]);

// Let's update the translation data

$product->fill([
    'description' => [
        'en' => 'Updated product description in English',
        'es' => 'Updated product description in Spanish',
    ],
    'details'     => [
        'en' => 'Updated product details in English',
        'es' => 'Updated product details in Spanish',
    ],
]);
$product->save();

Mass assignment with different data structure

$product = Product::create([
    'name'      => 'Amazon Kindle Oasis',
    'published' => true,
    'en'        => [
        'description' => 'Product description in English',
        'details'     => 'Product details in English',
    ],
    'es'        => [
        'description' => 'Product description in Spanish',
        'details'     => 'Product details in Spanish',
    ],
]);

// Once again, let's update the translation data

$product->fill([
    'en' => [
        'description' => 'Updated product description in English',
        'details'     => 'Updated product details in English',
    ],
    'es' => [
        'description' => 'Updated product description in Spanish',
        'details'     => 'Updated product details in Spanish',
    ],
]);
$product->save();
Edit this page on GitHub Updated at Wed, May 25, 2022