Suppose I have this:
//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')
My question is, how could I transfer:
$data["posts_related"] = Blog::posts_related($post->category_id,5);
To a view composer, since it seems I can't pass parameters to a view composer, but I can't be sure.
I appreciate any help!
In View Composer you can retrive data that passed to 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);
});
now everywhere you can use:
View::make('sidebars.related')->with(array('category_id' => 6));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With