PHP code example of bramalho / laravel-translations

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

    

bramalho / laravel-translations example snippets


BRamalho\LaravelTranslations\LaravelTranslationsServiceProvider::class,



namespace App;

use Illuminate\Database\Eloquent\Model;
use BRamalho\LaravelTranslations\Translate;

class Page extends Model
{
    use Translate;

    protected $fillable = ['title', 'body'];
}



use Illuminate\Database\Seeder;
use App\Page;
use BRamalho\LaravelTranslations\Translation;

class PageTableSeeder extends Seeder
{
    public function run()
    {
        Page::create([
            'id' => 1,
            'title' => 'Hello World!',
            'body' => 'This is my page'
        ]);

        Translation::create([
            'id' => 1,
            'translation_id' => 1,
            'translation_type' => App\Page::class,
            'language' => 'pt',
            'content' => [
                'title' => 'Olá Mundo!',
                'body' => 'Esta é a minha página'
            ]
        ]);
    }
}
sh
php artisan vendor:publish --provider 'BRamalho\LaravelTranslations\LaravelTranslationsServiceProvider'
sh
php artisan migrate
html
<h1>{{ $page->translation->content['title'] ?? $page->title }}</h1>
<p>{!! $page->translation->content['body'] ?? $page->body !!}</p>