PHP code example of afzaal565 / field-translations

1. Go to this page and download the library: Download afzaal565/field-translations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

afzaal565 / field-translations example snippets


use FieldTranslations\Traits\HasTranslations;

class YourModel extends Model
{
    use HasTranslations;
    
    protected $translatable = [
        'title',
        'description',
        // Add your translatable fields here
    ];
}

// Set translations
$model->setTranslation('title', 'en', 'English Title');
$model->setTranslation('title', 'es', 'Spanish Title');

// Get translations
$model->getTranslation('title', 'en'); // Returns: English Title
$model->getTranslation('title', 'es'); // Returns: Spanish Title

// Get all translations for a field
$model->getTranslations('title'); // Returns array of all translations

// Check if translation exists
$model->hasTranslation('title', 'en'); // Returns boolean
bash
php artisan vendor:publish --provider="FieldTranslations\Providers\FieldTranslationProvider"
bash
php artisan migrate