Here, i will let you know how to set global variable in laravel 5 application. In this article i will set globle variable two way on simple view share and another using composer view share.
As we know, every webiste or project there are several things are comman and access it in every page like footer text, logo, site title etc. So it is necessary to define it one time and access it on all views file.
So, In this example we will share varible from "AppServiceProvider.php"
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
view()->share('siteTitle', 'HDTuto.com');
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
view()->composer('*', function ($view) {
$view->with('siteTitle', 'HDTuto.com');
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
Now we can simple access $siteTitle varible like as bellow used :
How to use:
{{ $siteTitle }}
I hope you found your solution....
Do you like below Tutorials ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example