A new @action() directive

Submitted by laraning - 6 years ago

Sometimes you have a blade view, and you would like to render a certain HTML code only if your controller action is the one you passed in the directive.

In your App Service Provider you will create a new blade directive:

[app\Providers\AppServiceProvider.php]

public function boot() {
 
    Blade::if('action', function ($action) {
        if (Route::getCurrentRoute()->getActionMethod() == $action) {
            return $action;
        };
    });

}

Then in any blade view you can use it like this:

@action('index')
 <p>This is rendered only if I am in the controller action INDEX.</p>
@endaction

@action('store')
 <p>This is rendered only if I am in the controller action STORE.</p>
@endaction