Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

abarua's avatar

Show results of query in bootstrap modal

I want to show a list of query result inside a modal. Thanks in advance!

I have tried to modify my current modal to show the list but I am getting an undefined variable ($comments, in this case) error.

My modal:

<div class="modal" tabindex="-1" role="dialog" id="comment-post">
        <div class="modal-dialog" role="document">
         ...
                </div>
                <div class="modal-body">
                    <header><h3>other comments</h3></header>
                    @foreach($comments as $comment)
                        <article class="comment">
                            <p>{{ $comment->body }}</p>
                            <div class="info">Posted by {{ $comment->username }} on {{ $comment->created_at }}</div>
                        </article>
                    @endforeach
                    <form>
      ...
    </div>

My route:

Route::get('/commentboard',[
        'uses' => 'CommentController@getComment',
        'as' => 'commentboard'
    ]);
});

My getComment function inside the CommentController:

public function getComment()
    {
        $comments = Comment::orderBy('created_at','desc')->get();
        return view('commentboard',['comments' => $comments]);
    }

I want to show a list of comments in the modal window.

0 likes
2 replies
Cronix's avatar

So the view you are showing above with the modal, is the commentboard file?

return view('commentboard',['comments' => $comments]);
abarua's avatar

@CRONIX - Lol. Thank you so much for catching the mistake! I got it to work.

Please or to participate in this conversation.