PHP code example of tingo-gmbh / eloquent-translatable

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

    

tingo-gmbh / eloquent-translatable example snippets




namespace Tingo\Translatable\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Tingo\Translatable\Translatable;

class Entity extends Model
{
    use Translatable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'category',
        'description',
        'unit',
        'price',
    ];

    /**
     * The attributes that are translatable.
     *
     * @var array<string>
     */
    protected array $translatable = [
        'name',
        'category',
        'description',
    ];
}

$entity = Entity::create([
    'name' => 'Foo Entity',
    'category' => 'foo',
    'description' => 'This is my awesome entity!',
]);
$entity->createTranslation('name', 'Foo Entität', 'de');
$entity->createTranslation('name', 'L\'entité foo', 'fr');

$entity->updateTranslation('name', 'Aktualisierte Foo Entität', 'de');

echo $entity->getTranslation('name', 'de');
// Foo Entität
echo $entity->getTranslation('name', 'fr');
// L\'entité foo

App::setLocale('it');
$entity->createTranslation('name', 'Entità di Foo');
echo $entity->getTranslation('name');
// Entità di Foo

$entity->deleteTranslation('name', 'de');
bash
php artisan vendor:publish --provider="Tingo\Translatable\TranslatableServiceProvider" --tag="migrations"