Leverage Potential of Laravel ViewComposer to share data across views
In Laravel their is a core functionality to reuse the frequently used data through out your laravel application. So, in this post we’ll learn how you can leverage the Laravel ViewComposer method to share data across the views.
I’ll take an example of company information such as address, contact number and email etc which are used frequently and displayed on all the page of website. So I have created a model CompanyInformationModel to store company information.
app\Models\CompanyInformationModel.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class CompanyInformationModel extends Model { protected $table = 'company_information'; protected $primaryKey = 'id'; protected $fillable = ['company_name', 'customer_care_no', 'customer_care_email', 'company_address' ]; }
Then for composer we’ll create a simple class with name CompanyInfoComposer
which contains two methods and they are __construct()
and compose( View $view )
method.
app\Http\ViewComposers\CompanyInfoComposer.php
<?php namespace App\Http\ViewComposers; use App\Models\CompanyInformationModel; use Illuminate\View\View; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; class CompanyInfoComposer { protected $company_info = []; public function __construct( ) { } public function getCompanyInfo( ) { $this->company_info = CompanyInformationModel::first(); } // this method binds data to view public function compose(View $view) { $this->getCompanyInfo(); $view->with('company_info_composer', $this->company_info ); } }
In composer class CompanyInfoComposer
the property $company_info
contains a model instance of CompanyInformationModel
and the method getCompanyInfo( )
wil assign the CompanyInformationModel
object to $company_info
property.
The method compose(View $view)
binds the data to view called and it is called implicitly by the View::composer()
method. When using this in template or view called company_info_composer
as a variable and you’ll get the CompanyInformationModel
model instance.
Setting up the ViewServiceProvide
Asper Laravel documentation to use View::composer()
method it must be included within the boot()
method of service provider.
So create a service provider for views at location app\Providers\ViewServiceProvider.php.
<?php namespace App\Providers; use App\Http\ViewComposers\CompanyInfoComposer; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Illuminate\Support\Facades\View; class ViewServiceProvider extends ServiceProvider { public function boot() { View::composer( '*', CompanyInfoComposer::class ); } }
Register ViewServiceProvide in app.php
Whenever you create a service provider you have to register it into config\app.php within array providers
.
'providers' => [ '...', '...', App\Providers\ViewServiceProvider::class ]
Creating Controllers
I have created a ViewComposerController
in app\Http\Controllers\ViewComposerController.php with two methods home( Request $request )
and contact( Request $request )
.
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Log; use Illuminate\Http\Request; class ViewComposerController extends Controller{ public function home( Request $request ){ return view( 'view-composer.home', [] ); } public function contact( Request $request ){ return view( 'view-composer.about', [] ); } }
Create Views to display company information
So we have two view i.e view-composer.home and view-composer.about and these views are under folder resources\views\view-composer.
home.blade.php
<p>Company Name: {{ $company_info_composer->company_name }}</p> <p>Customer Care Number: {{ $company_info_composer->customer_care_no }}</p> <p>Customer Care Email: {{ $company_info_composer->customer_care_email }}</p> <p>Address: {{ $company_info_composer->company_address }}</p>
contact.blade.php
<p>Company Name: {{ $company_info_composer->company_name }}</p> <p>Customer Care Number: {{ $company_info_composer->customer_care_no }}</p> <p>Customer Care Email: {{ $company_info_composer->customer_care_email }}</p> <p>Address: {{ $company_info_composer->company_address }}</p>
Setup Web Routes
In routes\web.php
use App\Http\Controllers\ViewComposerController; Route::get( 'view-composer/home', [ ViewComposerController::class, 'home' ] )->name('view-composer.home'); Route::get( 'view-composer/contact', [ ViewComposerController::class, 'contact' ] )->name('view-composer.contact');
Output

Video
Extra Tips: Pass data to a specific view
View::composer( ['home'], CompanyInfoComposer::class );
In this case the company_info_composer
can only be accessible in view view-composer/home.blade.php.
Closure function within View::composer() method
If you don’t want to pass a class then insisted of that you can use a closure function and returnt the data you want.
View::composer('*', function ($view) { // return your data here });
Conclusion
So we have reached end of this post on Leverage Potential of Laravel ViewComposer to share data across views. If you like then Share this post.
Related Posts
- Laravel Jobs and Queues – Configuring, Sending Mail, Dispatching Jobs
- Laravel Sending Emails | The Easiest and Simple Way
























