mercredi 2 décembre 2015

Eloquent model 'with' call inside the controller

Let's say I have a Post model that hasMany Comment model. So basically to get the comments I would do $post->comments.

On some websites, I have seen people do this in the controller:

$post = App\Post::with('comments')->findOrFail($id);

return view('someview', compact('post'));

and then inside the view:

@foreach($post->comments as $comment) ...

To my understanding, $post->comments would always have the comments attached and there's no need to call with('comments'). Is this not correct?

If so, then what is the difference between the above and the below:

Controller

$post = App\Post::findOrFail($id);

return view('someview', compact('post'));

View

@foreach($post->comments as $comment) ....



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire