Laravel Translatable – Add translations to Eloquent models

To add translations to Eloquent models in Laravel, you can use a package like Astrotomic Laravel Translatable, which makes it easy to handle translations for your Eloquent models.

Step 1: Install the Laravel Translatable Package

Run the following Composer command to install the Astrotomic Laravel Translatable package:

bash

 

Step 2: Publish the Configuration File (Optional)

If you want to customize the configuration of the package, you can publish the config file using the following command:

bash

This will create a translatable.php configuration file in the config directory.

Step 3: Configure Your Model to Be Translatable

To make a model translatable, you need to:

  1. Add the Translatable trait to your model.
  2. Specify the fields that will be translatable.

Let’s assume you have a Post model and you want to make the title and content fields translatable.

In app/Models/Post.php, update your model as follows:

php

 

Step 4: Create a Translation Model and Migration

The translatable fields will be stored in a separate translations table, so you need to create a new model and migration for that. Let’s create the PostTranslation model.

Run the following command to generate a model and migration for translations:

bash

In the PostTranslation model (app/Models/PostTranslation.php), define it as follows:

php

In the migration file created (database/migrations/xxxx_xx_xx_create_post_translations_table.php), update it to define the schema for the post_translations table:

php

Run the migration to create the necessary tables:

bash

 

Step 5: Add Translations to Your Data

Now that everything is set up, you can add translations to your Post model in different languages. Here’s an example of how you can create a post with translations in both English and French:

php

 

Step 6: Retrieving Translations

When retrieving translations, the package automatically returns the model in the current application locale. You can change the locale dynamically or retrieve translations for a specific locale.

Example: Get Current Locale Translation

php

Example: Get Translation for a Specific Locale

php

Example: Get All Translations

php

 

Step 7: Updating Translations

To update translations for a post, use the same method translateOrNew():

php

 

Step 8: Deleting Translations

To delete a translation, use the deleteTranslation() method:

php

 


Summary

You’ve successfully integrated Astrotomic Laravel Translatable into your Laravel project. Here’s a quick summary of the steps:

  1. Install the package via Composer.
  2. Add the Translatable trait to your model and define translatable attributes.
  3. Create a translation model and migration.
  4. Add translations when creating models.
  5. Retrieve and update translations easily using the package methods.
  6. Use translateOrNew() to handle the addition and update of translations.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top