When user clicks on icon, ajax call to controller is called and controller returns some comments.
My controller
public function read($id)
{
$comments = Comment::where('post_id', $id)->get();
return response()->json([
'html' => view('includes.comments')->render(),
'comments' => $comments
]);
}
Ajax succes function
var comments_box = comments_container.find('.comments-box');
comments_box.html(data.html);
console.log(data);
In console log there is an array of objects with comments and rendered html view. But I can't iterate through that array. If i put some garbage code in comments.blade.php it shows it. But if I try
@foreach($comments as $comment) some code @endforeach It can't work at all, error message is Undefined variable: comments