1. Option: use AppServiceProvider
In this case, $ year is available for all views!
<?php namespace App\Providers; use Carbon; class AppServiceProvider extends ServiceProvider { public function boot() { view()->share('year', Carbon::parse()->year); } public function register() {
2. Option: use Composer View
In this case, the variable is available only for those species in which you need it.
Remember to add the newly created provider to config/app.php !
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Carbon; class ComposerServiceProvider extends ServiceProvider { public function boot() {
3. Use Blades @ inject-method
In views that need a year, you can enter a Carbon instance as follows:
@inject('carbon', 'Carbon\Carbon') {{ $carbon->parse()->year }}
Tim
source share