echosters / translatable
Translatable is Laravel Package helps you dealing with multi-lang tables
Installs: 1 187
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/echosters/translatable
README
Translatable is a Laravel package for dealing with Multi-Lang Tables.
Installation
Using composer to install translatable.
composer require echosters/translatable
DataBase
| id | title_en | title_ar |
|---|---|---|
| 1 | Hello | مرحبا |
Preparing your model
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Echosters\Translatable\Translatable; class Blog extends Model { use Translatable; public $translatedColumns = ['title']; }
Usage
//local = en $blog = Blog::find(1); $blog->title; // Hello //local = ar $blog = Blog::find(1); $blog->title; // مرحبا
Query
You May use whereLocale to query one of your translatable columns depending on the current locale
//local = en $blog = Blog::whereLocal('title', 'LIKE', '%hell%')->first(); $blog->title; // Hello //local = ar $blog = Blog::whereLocal('title', 'LIKE', '%مرح%')->first(); $blog->title; // مرحبا
Notes
just know that the title property doesn't exist in your table the trait will add it while the model is booting,
in other words the sql generated statement will be like below.
when locale is en statementselect title_en as title
when locale is ar statementselect title_ar as title
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.