Reputation: 2945
I am building an Laravel e-commerce site for Arabic and English languages.
Suppose I have Category table with title field. I want to insert title field in the two language. So I have created migration for title_en
and title_ar
.
<?php
namespace App;
use App\Traits\MultiLanguage;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use MultiLanguage;
protected $fillable = [
'title_en', 'title_ar',
];
/**
* This array will have the attributes which you want it to support multi languages
*/
protected $multi_lang = [
'title',
];
}
Here is multi language traits:
<?php
namespace App\Traits;
use Illuminate\Support\Facades\App;
trait MultiLanguage
{
public function __get($key)
{
if (isset($this->multi_lang) && in_array($key, $this->multi_lang)) {
$key = $key . '_' . App::getLocale();
}
return parent::__get($key);
}
}
But what to do when there are many table fields also, how to insert records. Is it possible?
Upvotes: 0
Views: 2176
Reputation: 2059
I dont think that having title_en
and title_ar
is a good idea. instead just have a title
and an extra column named lang
then query based on your language for instance
FooModel::where('lang', $lang)->get();
Upvotes: 1