Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

in database i have this:

name_en | 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
621 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...