Ad

Pass Data To All Routes?

- 1 answer

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);
});
Ad

Answer

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.

Ad
source: stackoverflow.com
Ad