PHP code example of zai / laravel-eloquent-multilingualization

1. Go to this page and download the library: Download zai/laravel-eloquent-multilingualization 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/ */

    

zai / laravel-eloquent-multilingualization example snippets


 Zai\Translate\TranslationServiceProvider::class,

// create a new article
$article = Article::create([
    'title' => 'Hello',
    'body' => 'laravel is awesome!'
]);

// add translation to the article
$article->addTranslation([
    'locale' => 'fr',
    'title' => 'Bonjour',
    'body' => 'laravel est génial!'
]);

public function store(Article $article)
{
    $article->addTranslation(request()->all();
}

// create a new title
$article = Article::create([
    'title' => 'Hello',
    'body' => 'laravel is awesome!'
]);

// for default locale, or no translations existing
$article->translation->title; // the output is Hello
$article->translation->body;  // the out is laravel is awesome!

// after adding translation
$article->addTranslation([
    'locale' => 'fr',
    'title' => 'Bonjour',
    'body' => 'laravel est génial!'
]);

// set the locale to fr
App::setLocale('fr');

$article->translation->title; // the output is Bonjour
$article->translation->body;  // the out is laravel est génial!

// for a no existing locale
App::setLocale('zh');

$article->translation->title; // the output is Hello
$article->translation->body;  // the out is laravel is awesome!

public function update(Article $article)
{
    $article->updateTranslation(request()->all());
}

$article->deleteTranslation('fr');

$article->deleteTranslations();

 $article->hasTranslations();

$article->hasTranslation();

class Article extends Model
{
    use Translatable;

    protected $with = ['translations'];

    protected $translatables = [
        'title',
        'body'
    ];
}

class Article extends Model
{
    use Translatable;

    protected $with = ['translations'];

    protected $appends = ['translation'];

    protected $translatables = [
        'title',
        'body'
    ];
}
command
php artisan translations:table

php artisan migrate
 php
protected $translatables = [
    'title',
    'body'
];