2017-06-01 54 views
0

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

$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 

但不知道如何获取数据到模板渲染。它只是呈现一个空白页。

+0

有什么错误? – JYoThI

+0

如果整个页面都是空白的,这听起来像是一个PHP错误。检查你的apache error.log – Brad

+0

也我觉得评论/索引应该是comments.index – Brad

回答

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 
1

使用这个代替:

$comments = Comment::all(); 

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

谢谢你的工作 – Kaley36