PHP code example of davesweb / laravel-translatable

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

    

davesweb / laravel-translatable example snippets




namespace App;

use Illuminate\Database\Eloquent\Model;

class Page extends Model
{
    protected string $translation = App\DifferentPageTranslationName::class;
}



namespace App;

use Davesweb\LaravelTranslatable\Models\TranslationModel;

class PageTranslation extends TranslationModel
{
    protected string $translates = App\SomeOtherModel::class;
}



$page = App\Page::query()->findOrFail(1);

$translations = $page->getTranslations();



$page = App\Page::query()->with('translations')->findOrFail(1);

$translations = $page->getTranslations();



use Illuminate\Database\Eloquent\Builder;

$locale = 'nl';

$page = App\Page::query()->whereHas('translations', function(Builder $query) use ($locale) {
    $query->where('locale', '=', $locale);
})->findOrFail(1);

$translations = $page->getTranslations();


$page = App\Page::query()->with('translations')->findOrFail(1);

$englishTranslation = $page->getTranslation('en');
$dutchTranslation   = $page->getTranslation('nl');



app()->setLocale('en');

$page = App\Page::query()->with('translations')->findOrFail(1);

$englishTranslation = $page->translation();



app()->setLocale('en');

$page = App\Page::query()->with('translations')->findOrFail(1);

$englishTitle = $page->translate('title');



app()->setLocale('en');

$page = App\Page::query()->with('translations')->findOrFail(1);

$dutchTitle = $page->translate('title', 'nl');