PHP code example of secretwebmaster / wncms-translatable

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

    

secretwebmaster / wncms-translatable example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Wncms\Translatable\Traits\HasTranslations;

class Post extends Model
{
    use HasTranslations;

    // Define the fields that can be translated
    public $translatable = ['title', 'description'];

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


$post = Post::find(1);

// Set translations for 'title' and 'description'
$post = Post::create([
    'title' => 'Hello World',
    'description' => 'Description in English',
]);

$post->setTranslation('title', 'fr', 'Bonjour le monde');
$post->setTranslation('description', 'fr', 'Description en Français');

// Get the 'title' based on the app's current locale
echo $post->title; // Outputs 'Hello World' or 'Bonjour le monde' depending on the locale

// Get the 'title' in French
echo $post->getTranslation('title', 'fr'); // Outputs 'Bonjour le monde'

// Get the 'description' in English
echo $post->getTranslation('description', 'en'); // Outputs 'Description in English'

$post->getTranslatable() // Outputs an array of translatable fields
bash
php artisan vendor:publish --provider="Wncms\Translatable\TranslatableServiceProvider" --tag="translatable-migrations"
bash
php artisan migrate