PHP code example of flobbos / laravel-translatable-db

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

    

flobbos / laravel-translatable-db example snippets


  $greece = Country::where('code', 'gr')->first();
  echo $greece->translate('en')->name; // Greece

  App::setLocale('en');
  echo $greece->name;     // Greece

  App::setLocale('de');
  echo $greece->name;     // Griechenland


Schema::create('languages', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('locale');
    $table->string('name');
}

Schema::create('countries', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('code');
    $table->timestamps();
});

Schema::create('country_translations', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('country_id')->unsigned();
    $table->string('name');
    $table->string('language_id')->index();

    $table->unique(['country_id','language_id']);
    $table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade');
    $table->foreign('language_id')->references('id')->on('languages')->onDelete('cascade');
});


Schema::create('languages', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('locale');
    $table->string('name');
}

Schema::create('countries', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('code');
    $table->string('name'); //the original name lives in this table
    $table->timestamps();
});

Schema::create('country_translations', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('country_id')->unsigned();
    $table->string('name');
    $table->string('language_id')->index();

    $table->unique(['country_id','language_id']);
    $table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade');
    $table->foreign('language_id')->references('id')->on('languages')->onDelete('cascade');
});

public $fallbackAttributes = ['name'];

// models/Country.php
class Country extends Eloquent {

    use \Flobbos\TranslatableDB\TranslatableDB;

    public $translatedAttributes = ['name'];
    public $fallbackAttributes = ['name'];
    protected $fillable = ['code'];
    //protected $fillable = ['code','name']; //if method 2 was used

    /**
     * The relations to eager load on every query.
     *
     * @var array
     */
    // (optionaly)
    // protected $with = ['translations'];

}

// models/CountryTranslation.php
class CountryTranslation extends Eloquent {

    public $timestamps = false;
    protected $fillable = ['name'];

}

    'use_db' => true,

    'language_model' => 'App\Whatever'

    'language_array'        => [
        'de' => ['name' => 'Deutsch', 'language_id' => '1'],
        'en' => ['name' => 'English', 'language_id' => '2'],
        'fr' => ['name' => 'Français', 'language_id' => '3']
    ]

    'use_fallback' => true,

    'fallback_locale' => 'de'

    'fallback_locale_id' => 1,

    'native_mode' => true

    'locale_key' => 'language_id',

    'locale_column' => 'locale',

    'to_array_always_loads_translations' => true,

    'middleware_default' => false



namespace MyApp\Models;

use Flobbos\TranslatableDB\TranslatableDB;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Country extends Eloquent
{
    use TranslatableDB\TranslatableDB;

    public $translationModel = MyApp\Models\CountryAwesomeTranslation::class;
}



namespace MyApp\Models;

use Flobbos\TranslatableDB\TranslatableDB;
use Flobbos\TranslatableDB\Contracts\PolyTrans;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Country extends Eloquent implements PolyTrans
{
    use TranslatableDB;

    public $translationModel = MyApp\Models\CountryAwesomeTranslation::class;
    //Set your polymorphic key here
    protected $translationForeignKey = 'translatable';
}

    'middleware_default' => false
bash
php artisan vendor:publish