PHP code example of ilyasapunkov / laravel-translatable

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

    

ilyasapunkov / laravel-translatable example snippets


    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use IlyaSapunkov\Translatable\Traits\Translatable;
    
    class Post extends Model
    {
        use Translatable;
    
        protected $translatableFields = ['title', 'description'];
    }
    

   $post = Post::create();
   
   $post->syncTranslation([
      'ru' => [
           'title' => 'Заголовок на русском',
           'description' => 'Описание на русском',
       ]
   ]);
    

    echo $post->title; // Заголовок на русском (если текущая локаль 'ru')
    echo $post->description; // Описание на русском
    

    $posts = Post::hasTranslation('title')->get();
    $posts = Post::hasTranslation('title', 'en')->get();