How do you create a custom Blade directive?
Answer
Clear, interview-ready explanation
Register a custom directive with the Blade facade’s directive method, normally from a service provider’s boot method. The registration gives the directive a name and a callback that receives the directive expression and returns the PHP that Blade should compile.
Custom directives are useful for small, repeated presentation transformations, such as formatting a date consistently. The generated PHP must be carefully quoted and tested because it becomes compiled template code. For more complex UI behavior, prefer a component, a view helper, or a dedicated formatter instead of creating a directive that hides substantial logic.