Search code examples
phplaravelrouteslaravel-5.3

Get route pattern by route name in laravel 5.3


In laravel 5.1, I was able to get the route path by route name, for example:

Defined Route:

Route::post('users/{user_id}/delete', 'UserController@delete')->name('user:delete');

In laravel 5.1, when I tried the below method, It gave the route without any error If I didn't pass any route parameter:

route('user:delete'); // Output: http://example.com/users/%7Buser_id%7D/delete

and then in javascript, I simply replaced %7Buser_id%7D with the user id dynamically. But laravel 5.3 is throwing error when accessing route by name that has parameters and I don't want to pass one, because the parameters are set dynamically from the javascript.

Is there any way I can access route pattern by route name like:

http://example.com/users/{user_id}/delete

Or

/users/{user_id}/delete

Thanks in advance.


Solution

  • You can give some route method some value, that will be then replaced in javascript. For example: route('user:delete', 'USER_ID'), then in javascript you will simply replace USER_ID.

    or the better way, is to use package called "Laroute"