我试图显示两列文章:已发布和未发布。文章分别通过控制器以两个数组的形式发送到视图( $ pubArticles 和 $ unpubArticles )。但是,视图partial使用相同的sub-partial来构建两个列 - 其中 $ articles 应该从数组中获取不同的值。我如何实现这一目标?
控制器
public function index(Article $article)
{
// published articles
$pubArticles = $article->latest('published_at')->published()->get();
// unpublished articles
$unpubArticles = $article->oldest('published_at')->unpublished()->get();
return view('articles.index', compact('pubArticles','unpubArticles'));
}
查看:articles / index.blade.php
@section('content')
<div class="col-md-6">
// i want to display published articles here
@include('articles._listall', [$pubArticles, 'publishText' => 'Published', ])
</div>
<div class="col-md-6">
// i want to display unpublished articles here
@include('articles._listall', [$unpubArticles, 'publishText' => 'Will be Published'])
</div>
@endsection
Sub-Partial:listall.blade.php
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-body no-padding">
@foreach ($articles as $article)
<div class="well">
<article>
<a href="{{ url('/articles/' . $article->id) }} ">
<h4>{{ $article->title }}</h4>
</a>
<p>{{ $article->body }}</p>
</article>
</div>
@endforeach
</div>
</div>
</div>