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!
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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?
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!
@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.
@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.
FatalErrorException in PagesController.php line 53:
Class 'App\Http\Controllers\Redirect' not found
@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.
Undefined property: App\Http\Controllers\PagesController::$redirector
Should I be importing something?
@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;
@mstnorris you say 'import it at the top' like what 'it' is is obvious... :)
use Illuminate\Support\Facades\Redirect;
@mstnorris Success! I had to import the URL façade too if you want to add that to your answer :) Thanks for your help.
@TimeSocks you're welcome.
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 ;)
@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.
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).
So what @bashy means is this
<form method="POST" action="{ { URL::route('test') } }#myId">
@bobbybouwmann Thank you @bobbybouwmann. For me, the best answer.
Another person given a bad way of doing something :( hopefully they come back and realise!
@bashy what is wrong with doing it that way?
@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?
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!
@bashy noted for the future :) thank you.
Nvm, got it. Override the response function and append the anchor tag to the URL returned by getRedirectUrl(). :)
@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
@bashy the method override was for the Form Request validation redirect :)
protected function getRedirectUrl()
{
return parent::getRedirectUrl() . '#anchor';
}
:)
@TimeSocks I know what it was for but with the other method, you don't need to do that. That's all I meant.
@bashy Ah, OK. Thank you :)
With Laravel 5 you can use :
redirect(url()->previous() . '#myanchor')
in Laravel 5+, to redirect back to page after posting, for example:
return redirect(url()->current() . "#anchor");
The solution changing the action of the form worked pretty good for me, thx!
Please or to participate in this conversation.