根据当前语言显示数据库列 [英] Displaying database columns based on current language

查看:104
本文介绍了根据当前语言显示数据库列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库中我有这样的:

in database i have this:

name_en | name_fr 

当用户选择法语 - 例如 - 我想要得到 name_fr 字段,如果他选择另一种语言,同样的事情

When the user select french language - for example - i want to get name_fr field, and the same thing if he chose another language

推荐答案

假设你在你的应用程序使用:

Assuming you set locale in your application using:

App::setLocale($lang);

如果您使用Eloquent,您可以添加到您的模型类accesssor:

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

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

以及mutator:

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

假设您将这些功能添加到内容 model现在可以使用:

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

这将导致显示和更改 name _ {$ lang} / code>如果你设置lang使用 setLocale

这篇关于根据当前语言显示数据库列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆