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

mvpop's avatar
Level 9

Get current route dinamically

I want to be able to do something like href="{{ route }}/{{ id }}/create" where {{ route }} is getting the current route.

I am generating a "add new" button on different views. Each of these views have a controller where I have the create method. I managed to get the id dynamically but I want to do the same thing for the route. Because If I hard code the path like /users/{{ id }}/create/ since this is dynamically generated on multiple views, I want to be able to point to the correct path {{ route }} instead of /users.

Hope it makes sense.

Thanks.

0 likes
8 replies
mvpop's avatar
Level 9

I have tried with Route::currentRouteName() but it is getting the entire view like this users.index/id.create which is invalid. I only need the users/ bit.

Thanks.

bobbybouwmann's avatar

I think this should work for you as well {{ url()->current() . '/' . $id . '/create' }}

1 like
mvpop's avatar
mvpop
OP
Best Answer
Level 9

Problem solved:

{{ \Request::segment(1) . '/' . $id . '/create'  }}
bobbybouwmann's avatar

What happens when you're URL looks like /users/images/{id}/create? This doesn't look like a solid solution, but it's up to you.

mvpop's avatar
Level 9

I am only using these tables on the routes where I have the /{{ route }}/{{ id }}/create method in the controller. (customers, vehicles, jobs etc.)

But, if needed I can also check if route name == somethindWhereTheUrlStructureIsDifferent then I modify the link for the button...

Please or to participate in this conversation.