PHP code example of s-shiryaev / laravel-translatable

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

    

s-shiryaev / laravel-translatable example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use SShiryaev\LaravelTranslatable\Translatable;

class Currency extends Model
{
    use Translatable;

    protected $translatable = ['name', 'code'];

    protected $fillable = [
        'id',
        'name_ru',
        'name_en',
        'code_ru',
        'code_en',
        'code_de',
        'active',
        'sort',
    ];
}

App::setLocale('ru');
$currency = new Currency(['name_ru' => 'Доллар', 'name_en' => 'Dollar']);
echo $currency->name; //Доллар

App::setLocale('ru');

$currency = Currency::find(1);
$currency->toArray(); //['name' => 'Доллар', 'name_en' => 'Dollar']

$currencies = Currency::all();
$currencies->toArray(); //[0 => ['name' => 'Доллар', 'name_en' => 'Dollar']]

$currency = Currency::find(1);
$currency->toArray(false); //['name_ru' => 'Доллар', 'name_en' => 'Dollar']

$currencies = Currency::all();
$currencies->toArray(false); //[0 => ['name_ru' => 'Доллар', 'name_en' => 'Dollar']]