Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MoFish's avatar

Custom Blade Directive (Passing Parameter In)

Hi all,

I have a custom blade directives for rendering a menu called @menu.

When i pass a static value of 'Main' it works fine.

When i pass a parameter of $name which is equal to 'Main' it does not work at all.

Does anyone have any ideas why this could be?

Thanks, Mofish

See the example code below:

Layout

@menu('Main')

AppServiceProvider

Blade::directive('menu', function ($name) {
    // alert $name;
    // $menu = Menu::where('name', "Main")->first(); 
    $menu = Menu::where('name', $name)->first(); 

    if($menu){ 
        echo "here"; 
    }
}); 
0 likes
4 replies
tykus's avatar

You are going outside the boundaries of the intended uses for Blade Directive; custom directives are intended to return a string of executable php code which is evaluated whenever the template is rendered. AFAIK, any other code in your Closure will be evaluated only at compile time, so is effectively cached.

Consider using Blade Components instead; this is a class/view based approach which will allow you to pass in data via attributes, and within the class you can fetch/arrange the data you need in the View, e.g.

// view template that consumes the Component
<x-menu :name="$name" />

Your component class could do something like the following to locate the Model from the database and pass it to a view:

class Menu extends Component
{
    public function __construct($name)
    {
        $this->name = $name;
    }

    public function render()
    {
        return view('components.menu', [
			'menu' => Menu::where('name', $this->name)->first()
		]);
    }
}
MoFish's avatar

Thank you for your response Tykus.

Ideally i would like to have have a component called 'render' which could be used in my views to display either menus or text for example:

<x-render::menu :name="$name" />
<x-render::text :name="$name" />

As i have never used these before, i'm playing around with them and have run the following artisans, however am a little confused where i should be creating the Render Component Class which will have the two functions inside. Also assuming the render will need to be placed into my boot function as per the documentation?

php artisan make:component Menu
php artisan make:component Item

Could you help clarify?

tykus's avatar
tykus
Best Answer
Level 104

The :: syntax is typically used for views/components that are provided by vendor packages, instead (within your own app) you would use a ., e.g.

<x-render.menu :name="$name" />
<x-render.text :name="$name" />

This represents the directory structure in the resources/views/components directory where you would have a render directory, and menu.blade.php and text.blade.php files. Similarly, the class would be located in a subdirectory of app/View/Components.

You can generate both class and view template using:

php artisan make:component Render\Menu

Please or to participate in this conversation.