Have a look at Ziggy
Get Route method
Just curious if there is a way to return the method expected from a route?
Laravel acts as a backend only and I would like to return in the response a links object which is easily done in a resource like:
'links'=> [
'delete' => route('user.destroy', ['user' => $this->id]),
],
But I would like to include the method expected so the frontend would not need to code the http method.
'links' => [
'delete' => [
'url' => route('user.destroy', ['user' => $this->id),
'method' => ??? How to get the expected method ???
]
],
Anyone know how or if this is possible?
That makes total sense now :)
As @snapey suggested, by looking at the logic behind the php artisan route:list artisan command, you can see how Laravel is getting info about a given route. See the source code here:
https://github.com/laravel/framework/blob/5.8/src/Illuminate/Foundation/Console/RouteListCommand.php
I did a quick test to see what's the result of $router->getRoutes() from the file above and it for sure returns a list of routes grouped by the HTTP methods (I created this temp route in my routes/web.php file):
Route::get('/test', function (\Illuminate\Routing\Router $router) {
dd($router->getRoutes());
});
TBH, I think what you have is clean enough and not sure if you need to complicate things. You just need to make sure to update the HTTP method for a route in 2 different places. That's up to you obviously.
Please or to participate in this conversation.