Custom blade directives and lookup tables

Many of us have been dealing with many role based systems in which we have to check on user types , toggle data based on permissions , defining middlewares and authorization classes

Laravel made it easy for us with its proper syntax and all the provided stuff out of the box

Let’s dive into details

Let’s pretend we have User and Role models as below

User.php

Role.php

Ok let’s pretend we have a drop-down list where items are showed based on user role and status

In our case we gonna check if a user is either superadmin or admin as below

That looks a little bit dirty especially if we have further conditions..

Here comes the beauty of Laravel blade directives

We will create a provider class named BladeRolesServiceProvider which will handle all the logic for us

BladeRolesServiceProvider.php

We go back again to out blade view and make some changes

That looks more readable isn’t it ?

Let’s play arround on our BladeRolesServiceProvider.php

Using lookup tables made our code more readable,understandable with getting rid of long switch if/else statements

I hope you enjoyed this article!