Category: Laravel, PHP

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.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners