How to set global view variables using composer share in Laravel?
In this post we will give you information about How to set global view variables using composer share in Laravel?. Hear we will give you detail about How to set global view variables using composer share in Laravel?And how to use it also give you demo for it if it is necessary.
Everyone will have same situation sometimes, like we have some information that we need to access in all view files for example site title, login user information, footer information etc.
But, we can define global view variables using composer() of Laravel. We just have to define value in AppServiceProvider and we can access that variable in every blade files. In bellow example i set “siteTitle” variable as global and i can access that variable in every view files.
You have to just open
AppServiceProvider.php and we can set variable as like bellow:
app/Providers/AppServiceProvider.php
namespace AppProviders;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
view()->composer('*', function ($view) {
$view->with('siteTitle', 'Itsolutionstuff.com');
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
}
}
Ok, now we can access “siteTitle” variable in every page like this way :
{{ $siteTitle }}
If you want to define global variable that can accessible in views, controllers, models etc then you have to follow this link : How to define Global Variables in Laravel 5?.
Hope this code and post will helped you for implement How to set global view variables using composer share in Laravel?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs