Answer the question
In order to leave comments, you need to log in
How to call a model method from a view?
Question about yii2. In the controller, I changed the data format from an object to an array of asArray() functions. $games = $games->asArray()->all();
After that, respectively, the methods of the model that were called in the form ceased to work. Because the array comes.
foreach ($games as $key => $game){
echo $game->pretty_date($game->date_add]);
}
Answer the question
In order to leave comments, you need to log in
1. You cannot use methods because you don't have a class you have an array, but there are no methods in it
2. For what purpose do you use an array? Do you select hundreds of thousands of records? Or what is your purpose for using it?
3. If you are already working with an array, then the $game->date_add construct does not make sense, because it's not an object, it can't have properties. You need to work with the array as $game['date_add']
4. Judging by the code, the pretty_date method formats the date in the form you need. That's just the question - why? if there is a built-in formatting mechanism - you can read more about it here
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question