Level 5
You can start following the documentation example:
https://laravel.com/docs/5.6/blade#extending-blade
Blade::directive('datetime', function ($expression) {
return "<?php echo ($expression)->format('m/d/Y H:i'); ?>";
});
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I've created a Blade directive
Blade::directive('img', function ($object) {
//
});
How can I pass an object to the function closure? it accepts only strings.
@img($object)
blade is building a string that will be inserted into your view. It does not run when the blade file is processed.
The string that is passed through needs to be returned from your blade to construct the runtime expression
Please or to participate in this conversation.