Before knowing how to share data to all views in Laravel, let’s know how to share data to a specific view file in Laravel.
Table of Contents
Share data to a specific view file in Laravel
To share data in views we usually pass an array of data from the controller or routes to views in a key-value pair. Let’s assume that there is a view file in Laravel’s resources/views folder named profile.blade.php and here we want to print the name.
Pass data to views
Now the value for the name is being sent to the view file in a key-value pair from the controller or route.
return view('profile', ['name' => 'Dev']);
Access value in profile view
To access the name in the view file, i.e., profile.blade.php, we can simply write the code below:
<?php echo $name; ?>
This way we can pass data to individual views but there may be a need to pass the same data to multiple view files. However, we can implement this in different ways.
Share data with all views in Laravel
Process 1: Use the view facade’s share method
We can use the view facade’s share method in the controller, the route or the service provider file.
Here is an example using this in app/Http/Controllers/Controller.php.
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\View;
class Controller extends BaseController
{
View::share('name', 'Dev');
}
Now all view files being returned from the methods of any class (which inherits the controller class) can access this key (here ‘name’). The code is given below to access the name.
<?php echo $name; ?>
Process 2: Use view composer
We can use View Composer when different view files need a particular piece of data. We can follow the below steps to implement this.
Step 1: Create Provider using the artisan command
php artisan make:provider ViewServiceProvider
This will create ViewServiceProvider. We can also make the necessary changes in the existing App\Providers\AppServiceProvider. But in this example, we are generating a separate service provider.
Step 2: Add code in App/Providers/ViewServiceProvider.php
Here we will share data in multiple view files (profile.blade.php and dashboard.blade.php)
<?php
namespace App\Providers;
use App\View\Composers\ProfileComposer;
use Illuminate\Support\Facades;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\View;
class ViewServiceProvider extends ServiceProvider
{
public function register(): void
{
// …
}
public function boot(): void
{
// View::composer(['profile', 'dashboard'], ProfileComposer::class);
View::composer('*', ProfileComposer::class);
}
}
If we want to share data in specific files, we will write:
View::composer(['profile', 'dashboard'], ProfileComposer::class);
If we want to share data in all files, we will write like this:
View::composer('*', ProfileComposer::class);
Step 3: Add the service provider to the providers array in the config/app.php configuration file
'providers' => […App\Providers\ViewServiceProvider::class,]
Step 4: Create ProfileComposer.php in app/View/Composers and add the following code
<?php
namespace App\View\Composers;
use Illuminate\View\View;
class ProfileComposer {
protected $user;
public function __construct() {
$this->user = ['name'=>'Dev'];
}
public function compose(View $view) {
$view->with('name', $this->user['name']);
}
}
Step 5: Add new routes and return a view file through route(routes/web.php) or controller
Route::get('/profile', function () { return view('profile'); }
Route::get('/dashboard', function () { return view('dashboard'); }
Step 6: Create view files profile.blade.php and dashboard.blade.php in resources/views
We can write the following code to access the name in resources/views/profile.blade.php:
<?php echo $name;?>
We can write the following code in resources/views/dashboard.blade.php:
<?php echo $name;?>
Now if we browse to http://<SITEURL>/profile or http://<SITEURL>/dashboard we will get the output as Dev.
You can learn more about Laravel View at: https://laravel.com/docs/10.x/views