PHP code example of jvdlaar / laravel-content-translation

1. Go to this page and download the library: Download jvdlaar/laravel-content-translation 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/ */

    

jvdlaar / laravel-content-translation example snippets


// config/app.php
'providers' => [
    ...
    JvdLaar\ContentTranslation\ContentTranslationServiceProvider::class,
    ...
];

// config/app.php
'aliases' => [
    ...
    'ContentTranslation' => JvdLaar\ContentTranslation\ContentTranslationFacade::class,
    ...
];


return [
  'fallback_language' => 'en',

  'country' => [
    'class' => \App\Models\Country::class,
    'label_property' => 'name',
    'properties' => [
      'name' => [''title' => ['

namespace App\Models;

use App\Base\Model;
use App\Contracts\TranslatableContract;
use App\Models\Traits\HasTranslatables;

class Country extends Model implements TranslatableContract {

  use HasTranslatables;

  protected $table = 'countries';
  protected $fillable = ['code', 'admin_name'];
  public $timestamps = FALSE;
  public $users = FALSE;



  /**
   * ATTRIBUTES
   */

  /**
   * Getter for 'name'.
   */
  public function getNameAttribute() {
    return $this->displayTranslation('name', TRUE);
  }

  /**
   * Getter for 'nationality'.
   */
  public function getNationalityAttribute() {
    return $this->displayTranslation('nationality', TRUE);
  }

  /**
   * Return an default for a property in this content.
   */
  protected function getTranslationDefault($property) {
    return $this->admin_name;
  }

}


Country::eagerLoadTranslations([1, 2, 3], 'nl');
dump(Country::find(1)->name);

\ContentTranslation::saveTranslation('country', $country->id, 'name', 'nl', 'Nederland');

$country->saveTranslation('nl', ['name' => 'Nederland', 'nationality' => 'Nederlander']);
 bash
php artisan vendor:publish --provider="JvdLaar\ContentTranslation\ContentTranslationServiceProvider"