E
E
Evgeny Romashkan2018-04-11 22:46:17
Laravel
Evgeny Romashkan, 2018-04-11 22:46:17

What is the best way to assemble a view with data from several models?

There is a small blog site on Laravel. Models Post and Comment, and respectively. PostsController and CommentController I show
the main page through the index method

$posts = Post::all();
        return view('blog.index', compact('posts'));

Page with a specific post via the show method
return $post ? view('blog.show_post', compact('post')) : abort(404);

in PostController. It is necessary to display comments to it on the page of a specific post.
Is it possible, and to what extent it will be correct in the PostsController show method to also access the comment model? Is this the correct use of a controller, doesn't it violate MVC principles?
A little confused.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2018-04-11
@EvgeniiR

For me, the controller and the model may not be related at all. You can call many models from one controller, and that's fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question