在刀片模板中显示Laravel集合

时间:2017-06-01 03:00:47

标签: laravel laravel-5 laravel-blade

我在刀片模板中显示集合时遇到问题。

$comments = Comment::all();

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

刀片的代码是:

@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

但不确定如何在模板中呈现数据。它只是呈现一个空白页。

2 个答案:

答案 0 :(得分:1)

请改用:

$comments = Comment::all();

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

答案 1 :(得分:0)

使用点表示法引用视图文件夹结构view('comments.index')。这表示文件resources/views/comments/index.blade.php。使用它。

@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