Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Foreach on collection in laravel ?

By M.K. Verma · 2 May 2022

Foreach on collection in laravel ?
$comments = $post->comments()->with('replyComments:replyComments')->orderBy('created_at', 'desc')->get();

This needs to add to the fetch code.

then

foreach ($comments as $comment) {
    echo $comment->replyComments;
};

Or else you can use array.

displayData = [];
foreach ($comments as $comment) {
    displayData[] = $comment->replyComments;
};
echo displayData;