Laravel 4,用参数查看作曲家?

时间:2013-04-23 20:26:26

标签: laravel laravel-4

假设我有这个:

//controllers/BlogController.php
$data["post"] = $post = Blog::recent_post();
$data["posts_related"] = Blog::posts_related($post->category_id,5);
return View::make('blog.home', $data);

//views/sidebars/related.blade.php
@foreach($posts_related as $r)
<p>{{ $r->name }}</p>
@endforeach

//views/blog/home.blade.php
@include('sidebars.related')

我的问题是,我怎么转移:

$data["posts_related"] = Blog::posts_related($post->category_id,5);

对于视图作曲家,因为我似乎无法将参数传递给视图作曲家,但我无法确定。

我感谢任何帮助!

1 个答案:

答案 0 :(得分:29)

在View Composer中,您可以检索传递给View的数据。

View::composer('sidebars.related', function($view)
{
   //$data contains category_id
   $viewdata= $view->getData();

   //Retrive based on category_id
   $posts_related = Blog::posts_related($viewdata['category_id'],5);

   //pass related post to view
   $view->with('posts_related', $posts_related);
});

现在你可以使用的地方:

 View::make('sidebars.related')->with(array('category_id' => 6));