Kaley36
Kaley36

Reputation: 273

Displaying A Laravel Collection in a blade template

I'm having issues displaying a collection in a blade template.

$comments = Comment::all();

return view('comments/index')->with(compact('comments'));

The code for the blade is:

@isset($comments)

@foreach($comments as $comment)
    <div>
        <p>
            <a href="{{ url('comments/', $comment->id) }}"><{{ $comment->commentor }}</a>
        </p>
    </div>
    <hr>
@endforeach
@endisset

@empty($comments)
    <div>
        <p>There were no comments available.</p>
        {{ $comments }}
    </div>
@endempty

But not sure how to get the data to render in the template. It just renders a blankpage.

Upvotes: 1

Views: 8031

Answers (2)

Mathieu Ferre
Mathieu Ferre

Reputation: 4412

Use this instead :

$comments = Comment::all();

return view('comments.index')->with(compact('comments'));

Upvotes: 1

Sandeesh
Sandeesh

Reputation: 11906

Use dot notation to reference the view folder structure, view('comments.index'). This represents the file resources/views/comments/index.blade.php. Use this.

@forelse ($comments as $comment)
    <div>
        <p>
            <a href="{{ url('comments/', $comment->id) }}">{{ $comment->commentor }}</a>
        </p>
    </div>
    <hr/>
@empty
    <div>
        <p>There were no comments available.</p>
    </div>
@endforelse

Upvotes: 0

Related Questions