Custom Blade directives in Laravel are very handy to allow some quick logic around an “expression” without having to pollute our views with verbose code, and allows to have them reachable in any view. What a directive must return is a string of inert PHP code but with a live the expression variable. The Laravel documentation points this is the way: This can become very cumbersome when you need to make logic that is more than just one line. Most IDE and Code Editors don’t help when you are using this kind of code, so we can’t optimally get code assistance when using this kind of strings.
Well, it’s just plain PHP code: This will allow to use this directive in our Blade views easily: The above will be compiled to and will be rendered as: This code is available in my Laratraits package for Laravel, with a lot of other useful tools.