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 ?
- Laravel - chmod(storage/oauth-private.key): Operation failed: Operation not permitted
- Laravel 7.x and 6.x datatables example from scratch
- How to compress PNG image using pngquant in PHP?
- Laravel 7.x and 6.x Chart example using Charts Package
- Guzzle http client GET and POST request example in Laravel 5
- Laravel schema default current timestamp value example
- Laravel 7.x and 6.x Get Site URL Examples
- Convert object to array in laravel 7.x and 6.x example