PHP code example of ahmedash95 / laravel-translatable

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

    

ahmedash95 / laravel-translatable example snippets


$newsItem = new NewsItem; // This is an Eloquent model
$newsItem
   ->setTranslation('name', 'en', 'Name in English');
   ->setTranslation('name', 'nl', 'Naam in het Nederlands');
   ->save();
   
$newsItem->name; // Returns 'Name in English' given that the current app locale is 'en'
$newsItem->getTranslation('name', 'nl'); // returns 'Naam in het Nederlands'

app()->setLocale('nl');

$newsItem->name; // Returns 'Naam in het Nederlands'

// config/app.php
'providers' => [
    ...
    Spatie\Translatable\TranslatableServiceProvider::class,

];

return [
  'fallback_locale' => 'en',
];

$newsItem->name;

public function getTranslation(string $attributeName, string $locale) : string

/** @var \Illuminate\Database\Eloquent\Model */
public $model;

/** @var string  */
public $attributeName;

/** @var string  */
public $locale;

public $oldValue;
public $newValue;

NewsItem::create([
   'name' => [
      'en' => 'Name in English'
      'nl' => 'Naam in het Nederlands'
   ],
]);

NewsItem::whereRaw('name->"$.en" = \'Name in English\'')->get();

NewsItem::where('name->en', 'Name in English')->get();

php artisan vendor:publish --provider="Spatie\Translatable\TranslatableServiceProvider"
 php
public function getTranslations(string $attributeName) : array
 php
public function setTranslations(string $attributeName, array $translations)
 php
$translations = [
   'en' => 'Name in English',
   'nl' => 'Naam in het Nederlands'
];

$newsItem->setTranslations('name', $translations);