Answer the question
In order to leave comments, you need to log in
How to dynamically convert an attribute?
There is a database in which such a structure: id | title_en | title_ru | ...
That is, one or another header should be taken from the choice of language.
I did this:
$items = Item::all();
foreach ($items as $item) {
$item->makeVisible('title_' . $request->lang); // lang беру из параметра, или header'а, или "en"
}
$items = Item::all();
foreach ($items as $item) {
$title = 'title_' . $request->lang;
$item->title = $item->$title;
}
Answer the question
In order to leave comments, you need to log in
The selected language must not be set in the controller.
That is, you need somewhere else before the controller, for example, in the service provider, get the language from request or from the session / database / configs and keep it available for receiving at any point.
Then implement the model method, it is possible in the form of a mutator, to get the value of the desired field based on the previously set language variable.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question