The issue you're encountering is due to a misunderstanding of where the macro is being applied. When you define a macro on the Route facade, it applies to the Route facade itself, not to individual route instances. This means that the macro is available on the Route facade, but not on the Illuminate\Routing\Route instance that is returned by the Route::get() method.
To achieve what you're trying to do, you need to define the macro on the Illuminate\Routing\Route class instead. Here's how you can do it:
- Update your macro definition in the
AppServiceProviderto target theIlluminate\Routing\Routeclass:
use Illuminate\Routing\Route;
public function boot()
{
Route::macro('myCustomMacro', function () {
// You can add any custom logic here
return $this;
});
}
- Now, when you define your route, you can chain the
myCustomMacromethod:
Route::get('/macro-test', [TestController::class, 'macroTest'])->myCustomMacro();
This should resolve the error, as the macro is now correctly applied to the Illuminate\Routing\Route instance, allowing you to chain it in your route definition.