PHP code example of stolkom / translatable

1. Go to this page and download the library: Download stolkom/translatable 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/ */

    

stolkom / translatable example snippets


use Stolkom\Translatable\Translatable;

public $translatableAttributes = ['name', 'description'];

$exampleModel->name // будет переведено, если значение перевода есть в БД 

public function getTranslation(string $field, string $locale = null)

$exampleModel->disableAutoTranslations(); 

Schema::create('example_model_translations', function (Blueprint $table) {
   $table->increments('id');
   $table->unsignedInteger('example_model_id')->index();
   $table->string('field');
   $table->string('locale', 2);
   $table->text('text')->nullable();

   $table->index(['example_model_id', 'field', 'locale']);
});

public $translationsTable = 'example_model_translations';

class ExampleModelTranslation extends Model
{
   public $timestamps = false;
   protected $guarded = [];
}

protected static function getTranslationModelName()
{
   return ExampleModelTranslation::class;
}

public function saveTranslations(array $translations)

['field_name' => ['locale' => 'value']] 

$exampleModel->saveTranslations([
   'name' => [
      'en' => 'Example',
      'ru' => 'Пример'
   ],
   'description' => [
      'en' => 'Description example',
      'ru' => 'Пример описания'
   ],
]);

ModelName::with('translations')->get(); 

protected $with = ['translations'];

php artisan vendor:publish --provider="Stolkom\Translatable\TranslatableServiceProvider"
php artisan migrate