Reputation: 63
I use a set of data on every route. Is there a way to pass this data to all routes without having to specify the data under each route, like:
Route::get('/', function()
{
$data = Data::all();
return View::make('index')->with('data', $data);
});
Route::get('/another', function()
{
$data = Data::all();
return View::make('another')->with('data', $data);
});
Upvotes: 2
Views: 1624
Reputation: 2474
Use view composers.
View::composer('profile', function($view)
{
$view->with('count', User::count());
});
Now each time the
profile
view is rendered, thecount
data will be bound to the view.
Also you need assign composer to many views, so you can just use ['profile', 'dashboard']
instead of 'profile'
.
Or you can share a data with one function:
View::share('name', 'Steve');
Upvotes: 3
Reputation: 5982
You can use view()->share()
in a service provider, like so :
view()->share('key', 'value');
You will then be able to access value
using {{ key }}
in all your views.
You can put it in the boot()
method of the default AppServiceProvider
.
Upvotes: 3