如何将同一数组变量下的多个数据发送到子部分?

时间:2016-08-16 05:33:38

标签: laravel laravel-5

我试图显示两列文章:已发布和未发布。文章分别通过控制器以两个数组的形式发送到视图( $ 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>

0 个答案:

没有答案