0

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.

1
  • Make sure the parameter is set before accessing the url. Commented Jan 30, 2017 at 13:42

1 Answer 1

1

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"

Sign up to request clarification or add additional context in comments.

8 Comments

how to user Laroute with dingo api routes?
You can use Laroute on every route, which has name. In your case like this: laroute.route('user:delete', { user_id : 1 });
I tried to generate the js file, but It only contains routes defined in web.php file, but doesn't include routes written in api.php, may be It's not including because I am using dingo routes.
Follow documentations. When you register route in Laravel, you will need to run php artisan laroute:generate this command to generate Javascript file with your routes. Don't forget this.
php artisan route:list this command shows you that dingo routes?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.