Keep every Blade view in sync—share once, use everywhere
Let’s get back to views. There are a few more concepts that we should cover, like how do you share data with all views? We’ll recap what we’ve done so far and take a look at an another option.
How Have We Passed Data to Views So Far?
The second argument of our view
method accepts an array of data. In the example below, we grabbed the data from the database and passed it to our view.
public function index()
{
$cars = PersonalCar::with(['brand', 'model'])->orderBy('year', 'desc')->get();
return view('personalcars/index', [
'title' => 'Personal Cars',
'cars' => $cars,
]);
}
If you don’t like to pass an array of data, you may utilize the with
method and pass the key/value pair as strings.
public function index()
{
$cars = PersonalCar::with(['brand', 'model'])->orderBy('year', 'desc')->get();
return view('personalcars/index')
->with('title', 'Personal Cars')
->with('cars', $cars);
}
How Do You Share Data With All Views?
Using the View::share
method allows for the developer to specify which content they want to share with all of the views:
View::share(‘key’, ‘value’);
The question now is, where do you put this? These go into a service provider. The simplest place is the boot
method of the AppServiceProvider
but we can generate another service provider and place the data there. For this instance, we’ll add the information to our App\Providers\AppServiceProvider
and we’ll cover how to create service providers later.
If you check your AppServiceProvider
you’ll notice that it’s blank. It’s just a skeleton for us to fill out.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
}
Let’s say that we want to share a title
to each of our views for whatever reason. We can specify that title in our boot
method. Make sure that you import your View
facade first in order to utilize it.
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
View::share('title', 'Laravel Tutorials');
}
}
Let’s take it for a spin. First, we’ll need to create a view: resources/views/share-data-with-all-views/index.blade.php
The Title of this is: {{ $title }}
Next, we need to create a route and call our view.
Route::get('/share-data-with-all-views', function () {
return view('share-data-with-all-views.index');
});
As you can see, no data is being passed to our view through the route. What happens when we visit the route? We get the following response: The Title of this is: Laravel Tutorials
.
That’s exactly what we were looking for.
Laravel Series
Continue your Laravel Learning.
Name routes your way
Laravel – P60: Resource Route Names
Part 60 of our Laravel fundamentals series unpacks how to customize resource route names for maximum clarity. Override default naming, prevent helper collisions, and keep large-scale projects organized with readable, intuitive route helpers and API endpoints.
Keep every Blade view in sync—share once, use everywhere
Laravel – P61: Sharing Data With All Views
In part 61 of our Laravel fundamentals series, learn techniques to share global data with every Blade view. Explore View::share, service-provider boot methods, view composers, and caching tips for consistent, performant layouts across your app.
Wire data into Blade with view composers
In part 62 of our Laravel fundamentals series, discover view composers—dedicated classes that push data into one or many Blade views automatically. Learn to register composers, keep controllers lean, and boost render performance.