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

TimeSocks's avatar

redirect()->back() with an anchor tag?

I have a contact form at the bottom of every page in my (Laravel 5) app. I can redirect the user back to the page they submit the form from with:

return redirect()->back();

but that defaults to the top of the page. How can I make it scroll automatically to the contact form at the bottom?

0 likes
30 replies
bobbybouwmann's avatar

At the moment you can't do it using back! However it is possible

return Redirect::to(Request::header('referer') . '#myId');

Note: I didn't tested this, but this will get you in the right direction!

1 like
TimeSocks's avatar

@bobbybouwmann that throws an error:

Class 'App\Http\Controllers\Redirect' not found

If I change it to:

redirect()->to($request->header('referer'). '#id');

It throws a 502 error.

1 like
mstnorris's avatar

@TimeSocks add this to whatever method you're calling when you submit the form.

$url = $this->redirector->getUrlGenerator();
return $url->previous() . '#whatever';

Take a look at the UrlGenerator API.

TimeSocks's avatar

@mstnorris

FatalErrorException in PagesController.php line 53:
Class 'App\Http\Controllers\Redirect' not found
mstnorris's avatar

@TimeSocks I was in the process of updating my answer.

My original method should still work though, just be sure to import it at the top.

TimeSocks's avatar

@mstnorris

Undefined property: App\Http\Controllers\PagesController::$redirector

Should I be importing something?

mstnorris's avatar
Level 55

@TimeSocks just use

return Redirect::to(URL::previous() . "#whatever");

And remember to import it at the top:

use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\URL;
12 likes
TimeSocks's avatar

@mstnorris Success! I had to import the URL façade too if you want to add that to your answer :) Thanks for your help.

bobbybouwmann's avatar

My answer was already correct, but you didn't import the correct classes.. Instead of coding in a trial and error way you should be fixing the issues ;)

TimeSocks's avatar

@bobbybouwmann well, I (kinda) asked you how to fix the issues but @mstnorris jumped in faster :) I do appreciate your input. I've only been learning Laravel (and coding in general) for a few weeks or so so I'm still feeling my way along to a certain extent.

bashy's avatar

What you can do is add the hash into the action="#idofform" part of the form and then just redirect back.

return back()->with('success', 'successfully sent');

This is what I do on my contact form: https://bashy.im/about (I used to have longer text here so it used to have to go down to the bottom).

8 likes
bobbybouwmann's avatar

So what @bashy means is this

<form method="POST" action="{ { URL::route('test') } }#myId">
8 likes
bashy's avatar

Another person given a bad way of doing something :( hopefully they come back and realise!

2 likes
TimeSocks's avatar

@bashy @mstnorris @bobbybouwmann My form action is pointing to a method in my PagesController that e-mails the form and then performs the redirect, so I imagine adding an anchor id in there wouldn't work anyway?

I have a related problem now however. I don't know if it's better off in a new thread, but i'll ask it here and then open a new thread if you deem it necessary.

I've just added a Form Request to handle validation of my mail form. This works just fine, but of course, when it redirects back to the page on a validation error, it loads the page at the top... Can I override the redirect URI so I could implement one of the methods you've supplied?

bashy's avatar

It's not wrong but it's not an "efficent" way of doing it.

For the error above, I'd definitely use the method of passing it along with the route/action value!

TimeSocks's avatar

Nvm, got it. Override the response function and append the anchor tag to the URL returned by getRedirectUrl(). :)

bashy's avatar

@mstnorris It's a great trick and simplifies things a lot!

@TimeSocks It works yeah, just don't see the need to import those classes and override the method just to do that :P

pmall's avatar
protected function getRedirectUrl()
{
    return parent::getRedirectUrl() . '#anchor';
}

:)

bashy's avatar

@TimeSocks I know what it was for but with the other method, you don't need to do that. That's all I meant.

kesm0's avatar

With Laravel 5 you can use :


redirect(url()->previous() . '#myanchor')
split19's avatar

in Laravel 5+, to redirect back to page after posting, for example:

return redirect(url()->current() . "#anchor");
1 like
vinesnts's avatar

The solution changing the action of the form worked pretty good for me, thx!

Please or to participate in this conversation.