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

laracoft's avatar

L8 Blade::directive(...) vs L9

Is there a way to keep writing this as 1 line in L9?

Blade::directive('mydirective', [MyDirective::class, 'non-static-method']);

It works L8, but in L9, it complains that 2nd argument is an array.

Thank you.

0 likes
13 replies
laracoft's avatar

Not sure how to make my question more prominent, but this is what I did:

Blade::directive('mydirective', Closure::fromCallable([MyDirective::class, 'static-method']));

Best answer will still be awarded to anyone with a 1 liner and non-static-method.

Thank you.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Ok figured out the syntax :) You need to pass a class not a string as the first element in the array

Blade::directive('mydirective', [new MyDirective, 'myMethod']);

or

Blade::directive('mydirective', [resolve(MyDirective::class), 'myMethod']);
laracoft's avatar

@Sinnbeck

Thanks, it did cross my mind to check the source, but I did not. The question I was pondering is why Laravel would make such a regressive change given that Route:: still accepts the array syntax. I'm curious what the issue might be, because I have the same L8 and L9 code running on PHP 8.1.

I think new and resolve will instantiate the class at registration and not only upon use, correct?

Sinnbeck's avatar

@laracoft Laravel didnt make any changes? That was what I pointed out. I cannot say how your original code ever worked, as it isnt valid! You need to pass a class instance and a method name.. Are you sure you didnt change it at all since it worked?

And yes it will instantiate the class (as it should) but not call the method.

laracoft's avatar

@Sinnbeck

I did not click the links, you said you don't see any difference. And I just checked, both links are using callable ..., what is the difference?

When we call Route::get("/url", [MyController::class, "method"]);, it is valid code, there is no instantiation, that's more efficient and is what I'm alluding to. Blade::directive(...) used to be like this as well.

Sinnbeck's avatar

@laracoft Exactly. Again Laravel didnt change anything. So asking for it to work like with a route would be a new feature. You can try making a PR to allow it if you want

laracoft's avatar

@Sinnbeck

Ok, I don't have time to get into the meat of it right now, but I used Blade::directive(...) to insert javascripts, the website would not have worked if those JS were missing, thus implying directive accepted arrays, there might be some explanation for all this.. when I get to it.

Sinnbeck's avatar

@laracoft If you are able to make a working version in laravel 8, I would love to have a look and try to figure out how it could have worked :)

laracoft's avatar

@Sinnbeck

Well, I agree with you it shouldn't have work. The question is, why did my website worked? Anyway, I need to get busy. Thank you for helping.

Sinnbeck's avatar

@laracoft Ah ok interesting to know :) And as I understood, it was a requirement that it was not static? So I assume it was static when you used laravel 8

Please or to participate in this conversation.