PHP code example of skelag / laravel-translatable-model

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

    

skelag / laravel-translatable-model example snippets


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBooksTable extends Migration
{
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->id();

            $table->date('written_at')->default(now());

            $table->timestamps();
        });
        
        $this->translates('books', ['author' => 'string', 'name' => 'string']);
    }

    public function down()
    {
        $this->dropTranslates('books');
        Schema::dropIfExists('books');
    }
}

namespace App\Models;

use SkelaG\LaravelTranslatableModel\Models\TranslatableModel;

class Book extends TranslatableModel
{
}

namespace App\Models;

use SkelaG\LaravelTranslatableModel\Models\TranslationModel;

class BookTranslation extends TranslationModel
{
    protected array $translatable = ['name', 'author'];
}

App::setLocale('ru');
$book = \App\Models\Book::create([
    'written_at' => \Carbon\Carbon::parse('1833-01-01'),
    'author' => 'Александр Сергеевич Пушкин',
    'name' => 'Евгений Онегин'
]);

App::setLocale('ru');
$book = new \App\Models\Book();
$book->author = 'Александр Сергеевич Пушкин';
$book->name = 'Евгений Онегин';
$book->save();

App::setLocale('en');
$book = \App\Models\Book::first();
$book->update(['author' => 'Alexander Sergeyevich Pushkin', 'name' => 'Eugene Onegin']);

App::setLocale('en');
$book = \App\Models\Book::first();
$book->author = 'Alexander Sergeyevich Pushkin';
$book->name = 'Eugene Onegin';