【发布时间】:2016-08-20 17:01:40
【问题描述】:
我正在使用 Laravel 5.2。
我的问题是:
变量可以在视图中分类吗?
例如:
有一个控制器,它可以获取当前用户的所有文章,像这样:
public function index()
{
$user=\Auth::user();
$articles = $user->articles;
return view('user.dashboard.index', compact('articles'));
}
看来,下面这些代码可以显示文章,像这样:
<div class="card">
<div class="card-header">
Articles
</div>
@if ($articles!=null)
<table class="table table-sm">
<thead>
<tr>
<th>title</th>
<th>created time</th>
<th>status</th>
</tr>
</thead>
<tbody>
@foreach ($articles as $article)
<tr>
<td>{{$article->title}}</td>
<td>{{$article->created_at}}</td>
<td>{{$article->status}}</td>
</tr>
@endforeach
</tbody>
</table>
@else
<p>Nothing</p>
@endif
</div>
这些文章可以分为两种类型:
1、“已发布”,状态为1.
2、“未发布”,状态为0。
问题:
我希望已发表的文章显示在卡片中(div class="card"),未发表的文章显示在另一张卡片中。
它们可以在视图中分类吗?因此,它不需要查询两次。
【问题讨论】:
-
为什么不在
foreach循环中测试status?类似@if ($article->status === 1) { 'published card' } @else { 'unpublished card' } @endif。 -
你能解释一下你对卡片的意思吗?