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



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


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!




Full Stack Laravel developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Hidden Reefs in String Pool, or Another Reason to Think Twice Before Interning Instances of String…

Demolishing the Machine Learning Misconception

How to Build an Assistant Using IBM Watson (Part 2 of 2)

Add more ternary operators

Are web apps becoming the new native apps?

A Byte of Coding Issue #154

How to build an app using Dash, Plotly and Python and deploy it to AWS

Spring WebFlux Security OTP Email with MongoDB and Flutter 1/2

Celebrating Progress: One Year into the Commons Stack Journey

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Full Stack Laravel developer

More from Medium

Running multiple Laravel commands with a single script

Why Laravel Is the Best PHP Framework — 10 Reasons to go with it!

Why should we use Laravel blade ?

Laravel Blade Template

Laravel Route 101: Basics & Best Practices