PHP code example of zoomyboy / laravel-translatable
1. Go to this page and download the library: Download zoomyboy/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/ */
zoomyboy / 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'
return [
'fallback_locale' => 'en',
];
$newsItem->name;
public function getTranslation(string $attributeName, string $locale) : string
$newsItem->setTranslation('name', 'en', 'Updated name in English');
$newsItem->save();
/** @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::where('name->en', 'Name in English')->get();
php
public function forgetAllTranslations(string $locale)
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);
php
// in your model
/**
* Convert the model instance to an array.
*
* @return array
*/
public function toArray()
{
$attributes = parent::toArray();
foreach ($this->getTranslatableAttributes() as $name) {
$attributes[$name] = $this->getTranslation($name, app()->getLocale());
}
return $attributes;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.