Answer the question
In order to leave comments, you need to log in
Is it good to use dynamic model properties in the view?
Good afternoon!
Started learning laravel. And immediately I want to learn as close as possible to the "correct variant".
The task is simple, there is a user and his articles The
question is in the title itself ...
If it is more detailed, then I don’t know which of the options is more correct (they both work)
option 1:
In the controller, get all the necessary data to display: The user, his articles
// почти псевдокод, но думаю суть понятна
$user = User:find(2);
$articles = $user->articles()
return функция_отображения
Answer the question
In order to leave comments, you need to log in
The first option is better. But if $articles
you just need to enter it on the screen, then I think it's even better like this:
// controller
$user = User::with('articles')->findOrFail($id);
// view
$user->articles // Без скобок! Данные уже были загружены и еще одного запроса к БД не будет
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question