DEV Community

Cover image for Creating Laravel Blade Directive
Jeffrey Kwade
Jeffrey Kwade

Posted on • Updated on

Creating Laravel Blade Directive

Hello fellow developers,

I'll be talking about creating your very own custom blade directives in Laravel.

Let's begin by explaining what blade directives are.

Laravel Blade directives are a powerful feature of the Laravel PHP framework that allows developers to write clean and concise templates with minimal boilerplate code. Here are some of the most commonly used Laravel Blade directives:

@if, @else, @elseif: These directives allow developers to conditionally display content based on a given condition.

@foreach: This directive allows developers to loop through an array or collection and display content for each item.

@include: This directive allows developers to include a partial view file within a parent view file, making it easy to reuse common markup across multiple pages.

@extends, @yield, @section: These directives allow developers to define a master layout file and inject content into specific sections of that layout from child views.

@auth, @guest: These directives allow developers to conditionally display content based on the user's authentication status.

@csrf: This directive generates a hidden input field with a CSRF token, which can help protect your application against cross-site request forgery attacks.

Laravel Blade directives are easy to learn and use, and they can help developers write clean, maintainable, and efficient code.

Now that we have that settled, let's create our custom blade directive.

We will be creating the @admin blade directive, this directive allows me to render an html element(s) only when a user is has administrative privileges.

We shall begin moving to the User model and create a method named "isAdmin".

public function isAdmin()
{
    return $this->is_admin == true;
}
Enter fullscreen mode Exit fullscreen mode

After we have successfully created this method, let's switch to the AppServiceProvider file, locate the boot function and let's register a function.

use Illuminate\Support\Facades\Blade;

public function boot(): void
{
    Blade::if('admin', function(){
            return auth()->user()->isAdmin();
        });
    }
Enter fullscreen mode Exit fullscreen mode

Make sure to import the blade facade. The first parameter of the blade facade is the name we are giving to the blade directive and the second parameter is a callabck function that simply returns the method we created within the User model.

Now Let's use our newly created @admin blade directive.

<div class="flex justify-end">
    @auth
        <div class="flex items-center gap-2">
             @admin
                  <a href="/admin" class="">Admin</a>
             @endadmin
                 <livewire:auth.logout />
        </div>
      @else
         <a href="/login">Login</a> &nbsp;/&nbsp; 
         <a href="/register">Register</a>
     @endauth
</div>
Enter fullscreen mode Exit fullscreen mode

If the current user has administrative privileges, the admin navigation link will be rendered.

Thank you for reading.
I'm Jeffrey Kwade, follow me on Twitter, Github and subscribe to my new Youtube channel

Top comments (0)