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

cooperino's avatar

How to pass current route id to the redirected route?

If I am currently on mysite/route_one/43 and there is a button that redirects to mysite/route_two. How can I pass the value 43 to /route_two?

Edit: There is a button in route_one with <a href="/route_two/" ..

0 likes
16 replies
Nakov's avatar

it is easy with named routes:

Route::get('route_one/{id}', function($id) {
		return redirect()->route('route_two', $id);
})->name('route_one');

Route::get('route_two/{id}', function($id) {
		dd($id);
})->name('route_two');
1 like
cooperino's avatar

@Nakov thanks! But I think it won't work in my case, I edited the post with more code (I am using href), also, route_two already has its own id. should I just add another one /{id}/{redirected_from_id}?

Nakov's avatar

@cooperino try this:

<a href="/route_two/{{ request('id') }}"

of course your parameter in the url should be named id for the above to work.

1 like
ehab.aboshehab's avatar

@cooperino What are you using ? laravel ? vue ? jquery ? js ? please share you code

If you are in laravel blade or controller .. you will need to name your second route :

Route::get('second_route', [Controller::class, 'method'])->name('second_route');

and then use it in the controller or the blade and pass the id as a query_string

route('second_route).'?id='.$id

let your second route accept parameters such id

Route::get('second_route/{id}', [Controller::class, 'method'])->name('second_route');
route('second_route, $id);
1 like
J8RO8N's avatar

Inside your controller you can get the parameter and pass it down to your view and append it to the button / link

1 like
cooperino's avatar

@jeroenrosendaal it's href="/route_two/{id} <- the id is not the id of route_one, route_two already has an id, should I just add href="/route_two/{id}/{redirected_from} or some sort?

J8RO8N's avatar

What does your routes, controller and view file look like?

1 like
Snapey's avatar

just pass it in query string

<a href="/route_two/67?from=45"

and then check for it in the controller

if($request->has('from') {
1 like
cooperino's avatar

@Snapey thank you! I thought there is some Laravel way to do it, but this seem the most convenient way

1 like
Snapey's avatar

@cooperino you could make it part of the route, but then you would need to make it optional

obviously be aware that the user can mess about with it

personally i always use named routes so like

<a href="{{route('product.show',['id'=>$product->id, 'from'=>$original->id])}}">

the route function will make unexpected parameters part of the querystring

1 like
cooperino's avatar

@Snapey Then if I leave it the simple way as you showed above it should be fine then right? Unless I'd need a UTM tracking library which I'm not sure what it is (But it is actually working right now)

Please or to participate in this conversation.