Displaying database columns based on current language

ぐ巨炮叔叔 提交于 2019-11-28 11:47:59

Assuming you set locale in your application using:

App::setLocale($lang);

if you use Eloquent, you can add to your model class accesssor:

public function getNameAttribute($value) {
    return $this->{'name_'.App::getLocale()};
}

and also mutator:

public function setNameAttribute($value) {
    $this->{'name_'.App::getLocale()} = $value;
}

Assuming you added those functions to Content model you can now use:

$content = Content::first(); // find first article
echo $content->name;  // displaying its name

$content->name = 'updated content'; // changing its name
$content->save(); // saving

This will cause displaying and changing name_{$lang} if you set lang using setLocale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!