PHP code example of yaim / multilingual-eloquent

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

    

yaim / multilingual-eloquent example snippets


  $author = Author::first();
  echo $author->email; // [email protected]
  echo $author->name; // Sun Tzu

  $author = Author::locale('zh_Hans')->first();
  echo $author->email; // [email protected]
  echo $author->name; // 孫子

  $author = Author::create([
    'email' => '[email protected]',
    'name' => 'Sun Tzu',
  ]);

  echo $author->name; // Sun Tzu
  
  $author->setLocale('zh_Hans')->update([
      'name' => '孫子',
  ]);

  $author = Author::locale('zh_Hans')->first();
  echo $author->name; // 孫子

Schema::create('authors', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('email');
    $table->timestamps();
});

Schema::create('author_translations', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('author_id');
    $table->string('language_code');
    $table->string('name');
    $table->text('bio');
    $table->timestamps();

    $table->foreign('author_id')
          ->references('id')->on('authors');
});

// app/Author.php

use Yaim\MultilingualEloquent\Database\Eloquent\MultilingualModel;

class Author extends MultilingualModel {
    
    protected $fillable = [
        'email',
        'name',
        'bio',
    ];

    protected $translatable = [
        'name',
        'bio',
    ];
}

// app/Post.php

use Yaim\MultilingualEloquent\Database\Eloquent\MultilingualModel;

class Post extends MultilingualModel
{
    // default translations table name => 'post_translations'
    protected $translationTable = 'multilingual_posts';

    // default translation foreign key name => 'post_id'
    protected $translationForeignKey = 'multilingual_post_id';

    // default language code key name => 'language_code'
    protected $languageCode = 'translation_language_code';

    protected $fillable = [
        'title',
        'content',
    ];

    protected $translatable = [
        'title',
        'content',
    ];
}