You can use View::share('top_comments', Comment::take(3)->get());
to make it available everywhere. Of course, you'll have to place it some place where it gets loaded no matter what page you load if you want it in every possible view. (One such place would be in the __construct()
method of your BaseController
, but I doubt that could be considered a best practice. Not sure where I'd put it myself.)
Another way would be to leverage view composers, like this:
View::composer('master', function($view)
{
$view->with('top_comments', Comment::take(3)->get());
});
This works if you meant that you want it in your master.blade.php
no matter from where it is loaded, because it's bound to that view. If you choose this option, I recommend for instance creating a file composers.php
and including that in app/start/global.php
.
That said, I assume your controller sample above left something out, because it looks like it's missing a method declaration.