Laravel Create Custom Blade Directive

In this tutorial you will learn about the Laravel Create Custom Blade Directive and its application with practical example.

In this Laravel Create Custom Blade Directive tutorial I will show you how to create custom blade directive in laravel. In this tutorial you will learn to create custom blade directives in laravel. In this article I will share example to create custom blade directives.

In laravel you can create your own @var, @if, @case directive that help you to avoid rewrite same code multiple time so that you can reuse.

Laravel Create Custom Blade Directive

In this step by step tutorial we will be creating custom blade directives and reuse it in blade file. we will create @nl2br blade directive and use it with example. Please follow the instruction given below:

Step 1: Create Custom Blade Directive

First of all you have to declare custom blade directive in your app service provide file:

app/Providers/AppServiceProvider.php

Step 2: Create Route

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Step 3: Create Blade File

Now you can use @nl2br directive in this blade file as following:

In this tutorial we have learn about the Laravel Create Custom Blade Directive and its application with practical example. I hope you will like this tutorial.