PHP code example of fevrok / laravel-translatable

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

    

fevrok / laravel-translatable example snippets


Fevrok\Translatable\TranslatableServiceProvider::class,

return [
   /**
    * Set whether or not the translations is enbaled.
    */
   'enabled' => true,

   /**
    * Select default language
    */
   'locale' => 'en',

];


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

class Item extends Model
{
    use Translatable;

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

class CustomTranslation extends \Fevrok\Translatable\Models\Translation
{
    protected $table = 'custom_translations';
}

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

class Item extends Model
{
    use Translatable;

    /**
      * The attributes that are Translatable.
      *
      * @var array
      */
    protected $translatable = [
        'name'
    ];

    /**
      * The model used to get translatios.
      *
      * @var string
      */
    protected $translations_model = CustomTranslation::class;
}

// Loads all translations
$posts = Post::with('translations')->get();

// Loads all translations
$posts = Post::all();
$posts->load('translations');

// Loads all translations
$posts = Post::withTranslations()->get();

// Loads specific locales translations
$posts = Post::withTranslations(['en', 'da'])->get();

// Loads specific locale translations
$posts = Post::withTranslations('da')->get();

// Loads current locale translations
$posts = Post::withTranslations('da')->get();

echo $post->title;

echo $post->getTranslatedAttribute('title', 'locale', 'fallbackLocale');

$post = $post->translate('locale', 'fallbackLocale');
echo $post->title;
echo $post->body;

// You can also run the `translate` method on the Eloquent collection
// to translate all models in the collection.
$posts = $posts->translate('locale', 'fallbackLocale');
echo $posts[0]->title;

// with string
if (Translatable::translatable(Post::class)) {
    // it's translatable
}

// with object of Model or Collection
if (Translatable::translatable($post)) {
    // it's translatable
}

$post = $post->translate('da');
$post->title = 'foobar';
$post->save();

$page = Page::whereTranslation('slug', 'my-translated-slug');
// Is the same as
$page = Page::whereTranslation('slug', '=', 'my-translated-slug');
// Search only locale en, de and the default locale
$page = Page::whereTranslation('slug', '=', 'my-translated-slug', ['en', 'de']);
// Search only locale en and de
$page = Page::whereTranslation('slug', '=', 'my-translated-slug', ['en', 'de'], false);
bash
php artisan vendor:publish --provider="Fevrok\Translatable\TranslatableServiceProvider"
bash
php artisan migrate