Kamran G. Kamran G. - 1 year ago 149
PHP Question

Laravel @foreach

I need to get each comments of news . Its working good for firstorFail() item


But bring me empty result when im try this one with foreach:

@foreach($news->comments() as $comment)

Answer Source

This: $news->comments() bringing you query builder probably. So what you need is to ->get() for execute query like this:

@foreach($news->comments()->get() as $comment)


or as a @Roy Philips mentioned: $news->comments