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

lat4732's avatar
Level 12

How to redirect through anchor element to a route with session message

Hey everyone!

I want to somehow represent this:

redirect()->route('user.dashboard')->with('section', 'bank');

inside the href attr of an anchor element

<a href="{{ route('user.dashboard') }}"></a>

but I'm not really sure it can happen the way I think. I could also use javascript for the redirection but I cannot find a way to set Laravel session message inside that redirection. Any ideas how to make this work?

0 likes
9 replies
CODE-AXION's avatar

maybe this can help !

return redirect()->route('user.dashboard')->withFragment('myfragment')->with('section', 'bank');

in blade

<div id="myfragment"> hi there </div>

lat4732's avatar
Level 12

@CODE-AXION I'm not really sure you understand what I'm trying to achieve.

CODE-AXION's avatar

@Laralex


    return redirect()->route('your route')
                    ->with('message',"Your message. <a href='{ url('/posts') }'>Click here</a>
                    ->with('status', 'success');

tykus's avatar

@laralex you could use a query param instead:

<a href="{{ route('user.dashboard', ['section' => 'bank']) }}"></a>

Then in the Controller action; you can get the section either from the Session or from the Request:

$section = session('section', request()->get('section'));
lat4732's avatar
Level 12

@tykus That's not a solution for me. Any other ideas? Thanks for your time tho.

tykus's avatar

@Laralex in that case, what you are attempting is not possible unless the Session already has the section key before the view with that link is rendered.

1 like
Sinnbeck's avatar

So link to a route where you then return this redirect()->route('user.dashboard')->with('section', 'bank');

lat4732's avatar
Level 12

@Sinnbeck Yeah, but this costs me a declaration of a whole route just for a redirection which I don't think is the best option.

Please or to participate in this conversation.