PHP code example of said / laravel-translatable

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

    

said / laravel-translatable example snippets




namespace App;

use Illuminate\Database\Eloquent\Model;
use Said\Translatable\Traits\Translatable;

class MyModel extends Model
{
    use Translatable;
    protected $translatable_columns=[
        'name'
    ];
    protected $fillable=[
        'price'
    ];
}



public function store(Request $request) {
    $data = [
       'en' => [
           'name'       => en-name
       ],
       'fr' => [
           'name'       => fr-name
       ],
    ];

    // Now just pass this array to regular Eloquent function    
    MyModel::create($data);
}

// assuming $myModel is an instace of MyModel class defined above
// and the translations are set
echo $myModel->name; // returns 'Product'
App::setLocale('fr');
echo $myModel->name; // returns 'Produit'

$myModel->in('fr')->translate('name'); // returns 'Produit'