PHP code example of konnco / laravel-transeloquent

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

    

konnco / laravel-transeloquent example snippets


composer 

php artisan vendor:publish

php artisan migrate

return [
    // default locale
    'locale' => 'en',
    
    // transeloquent model
    'model' => Konnco\Transeloquent\models\Transeloquent::class
]; 

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class News extends Model {
    use \Konnco\Transeloquent\Transeloquent;
}

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Konnco\Transeloquent\Transeloquent;

class News extends Model {
    use Transeloquent;
    
    protected $translateOnly = ['translate-field-1', 'translate-field-2'];
}

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Konnco\Transeloquent\Transeloquent;

class News extends Model {
    use Transeloquent;
    
    protected $translateExcept = ['dont-translate-1', 'dont-translate-2'];
}

//in the original language
$post = Post::first();
echo $post->title; // My first post

App::setLocale('id');
$post = Post::first();
echo $post->title; // Post Pertama Saya

App::setLocale('id');
$post = Post::first();
$post->title = "Post Pertama Saya";
$post->save();

// or set locale for specific model

$post = Post::first();
$post->setLocale('id')
$post->title = "Post Pertama Saya";
$post->save();

$post = Post::first();
$post->translationExist('id'); //return boolean