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

adamsrog's avatar

Routing back() but adding a URL parameter

Have a UI that has some tabbed content. A tab can be specified to be active (displayed) by using a URL parameter.

The use case is as follows:

  • Users loads page. (i.e. foo.com/mypage)
  • Clicks on the tab where they need to add a note.
  • Submits and the note is created.
  • Routes back()->with('success', 'blah')

The problem I'm having is that I'd prefer to route back() to the page after creating a note, but include a URL parameter so the tab the user was just working in is automatically displayed and they can see their note. (i.e. foo.com/mypage?tab=notes). Is that possible or must one route('my.route.path', ['tab' => 'notes']) like this?

I've looked through documentation and perhaps I'm just missing something simple. Or am I approaching this seemingly simple issue incorrectly?

0 likes
4 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

One solution to this problem is to use the redirect() method instead of back(). You can pass the URL with the desired parameter as an argument to the redirect() method. Here's an example:

return redirect()->to(url()->previous() . '?tab=notes')->with('success', 'blah');

This code will redirect the user back to the previous page with the tab parameter set to notes. The with() method is used to flash a success message to the session.

Note that url()->previous() will return the URL of the previous page, so this code will work even if the user came to the current page from a different URL.

adamsrog's avatar

Amazing, thank you AI. This worked beautifully.

For the record, ChatGPT sent me into the weeds when I tried asking this question various different ways.

All hail Lary!

adamsrog's avatar

@Snapey Thanks, I just checked it out. Hadn't considered adding the hash to the form's action. Very cool.

Please or to participate in this conversation.