<?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'
];
}