Great questions! Let’s break down each one:
1. Does back() execute a redirection, or just back to the previous page?
Yes, back() in Laravel does execute a redirection. It returns a redirect response to the previous URL (usually from the Referer header).
return back();
This will tell the browser (or Inertia) to navigate to the previous page.
2. What’s the difference between to_route() and back()?
to_route('users.index')
- Redirects the user to a named route, in this example, the
users.indexroute. - Always goes to a specific route.
back()
- Redirects the user back to the previous page (from where the request was made).
- Not route-specific — it just says “go back”.
Example:
// To a specific route
return to_route('users.index');
// Back to previous page
return back();
3. If back() doesn’t execute a redirection, is the example above not appropriate?
back() does execute a redirection, but it’s important to note how the HTTP status code works, especially for non-GET requests.
For POST, PUT, PATCH, DELETE requests:
Browsers typically remember the last request’s method. If you respond to a POST with a regular 302 redirect, the browser could re-POST when going back, which is not what you want.
That’s why Inertia (and Laravel's Inertia adapter) converts redirects after non-GET requests to a 303 status — this tells the browser to follow up with a GET request.
You generally don’t have to worry about this yourself if you’re using Laravel’s Inertia adapter:
back()andto_route()both will perform a redirect, and Inertia will make sure the correct 303 status code is used for non-GET requests.
Summary Table
| Method | Where it redirects | Use-case |
|---|---|---|
return back(); |
Previous page (via browser referer) | After POST/PUT/DELETE when you want to return user back |
return to_route('x') |
To the specific named route | When you want to send the user to a given location |
Practical Inertia Usage Example
public function store(Request $request)
{
$user = User::create($request->validated());
Inertia::flash('message', 'User created successfully!');
// Option 1: Redirects to the previous page
return back();
// Option 2: Redirects to a specific route
// return to_route('users.index');
}
Both are fine — choose based on your UX needs!
Laravel’s Inertia adapter will handle the status code (including 303 for appropriate requests).
References:
If you have a specific scenario, let me know!