1. Go to this page and download the library: Download novius/laravel-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/ */
novius / laravel-translatable example snippets
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->translatable(); // Macro provided by the package
$table->string('title');
$table->text('text');
$table->timestamps();
});
namespace App\Models;
use \Illuminate\Database\Eloquent\Model;
use Novius\LaravelTranslatable\Traits\Translatable;
class Post extends Model {
use Translatable;
...
}
$post = new Post([
'title' => 'Français',
]);
$post->save()
$post->translate('en', ['title' => 'English']);
$post->translate('es', ['title' => 'Español']);
// All translation including `fr`
$allTranslations = $post->translations;
$englishTranslation = $post->getTranslation('en');
// $italianTranslation is null
$italianTranslation = $post->getTranslation('it');
namespace App\Models;
use \Illuminate\Database\Eloquent\Model;
use Novius\LaravelTranslatable\Traits\Translatable;
class Post extends Model {
use Translatable;
protected function translateAttributes($parent): void
{
$this->some_attribut = $parent->some_attribut.' translated';
}
...
}