How to update translatable model in laravel?

Member

by lizzie , in category: Third Party Scripts , 7 months ago

How to update translatable model in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , 7 months ago

@lizzie 

To update a translatable model in Laravel, you can follow these steps:

  1. Retrieve the model instance using its ID or any other unique identifier. For example:
1
$model = YourModel::findOrFail($id);


  1. Update the main attributes of the model using the update() method. For example:
1
2
3
4
5
$model->update([
    'title' => 'New Title',
    'content' => 'New Content',
    // Update other main attributes here
]);


  1. To update the translatable attributes of the model, you can use the setTranslation() method. For example, if you have a translatable attribute called name, you can update it like this:
1
2
3
$model->setTranslation('name', 'en', 'New Name');
$model->setTranslation('name', 'fr', 'Nouveau Nom');
// Update other translatable attributes here


  1. Finally, save the changes to the model by calling the save() method:
1
$model->save();


By following these steps, you can easily update a translatable model in Laravel.