A Laravel package for model attributes translations
You can install the package via composer:
composer require syscape-space/laravel-translation
- publish the migration to create the media table by running
php artisan vendor:publish --provider="SyscapeSpace\LaravelTranslation\LaravelTranslationServiceProvider" --tag="migrations"
.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use SyscapeSpace\LaravelTranslation\Traits\hasTranslation;
class Post extends Model
{
use HasFactory, HasTranslation;
// to force laravel accept the translation attributes
protected $guarded = [];
#or if you have time
protected $fillable = ['title','title_en','title_ar','content','content_en','content_ar'];
//-------------------------------------------------
// just like that without any migration or configuration
public $translationAttributes = [
'title',
'content',
];
}
#any where in your code
// depending on the current locale
$post = Post::create([
'title' => 'title',
'body' => 'content',
]);
#or
$post = Post::create([
'title_ar' => 'عنوان',
'title_en' => 'title',
'body_ar' => 'محتوى',
'body_en' => 'content',
]);
# and
$post->title; // title
#or
$post->title_ar; // عنوان
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email ahmedalmory02@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.