vincent15000's avatar

InertiaJS and redirection after POST, PUT and DELETE

Hello,

Hmmm ... Let's have a look at this page about flash datas.

https://inertiajs.com/docs/v2/data-props/flash-data#flash-data

public function store(Request $request)
{
    $user = User::create($request->validated());

    Inertia::flash('message', 'User created successfully!');

    // Or flash multiple values at once...
    Inertia::flash([
        'message' => 'User created!',
        'newUserId' => $user->id,
    ]);

    return back();
}

Now let's have a look at this page about redirection.

https://inertiajs.com/docs/v2/the-basics/redirects#redirects

public function store(Request $request)
{
    User::create(
        $request->validate([
            'name' => ['required', 'max:50'],
            'email' => ['required', 'max:50', 'email'],
        ])
    );

    return to_route('users.index');
}

When redirecting after a PUT, PATCH, or DELETE request, you must use a 303 response code, otherwise the subsequent request will not be treated as a GETrequest. A 303 redirect is very similar to a 302 redirect; however, the follow-up request is explicitly changed to a GET request. If you’re using one of our official server-side adapters, all redirects will automatically be converted to 303 redirects.

Well ... Not sure to understand all what is said.

Does back() execute a redirection ? or just back to the previous page ?

What difference between to_route() and back() ?

If back() doesn't execute a redirection, is the example above not appropriated ?

Thanks for your reply ;).

V

0 likes
3 replies
LaryAI's avatar
Level 58

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.index route.
  • 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() and to_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!

1 like
vincent15000's avatar

I think that I can trust Lary's reply, but I already asked for the same question to Claude and ChatGPT and I received contradictory replies in comparison with Lary's reply.

Can somebody confirm that Lary's reply is right ?

Thanks ;).

martinbean's avatar
Level 80

Does back() execute a redirection ? or just back to the previous page ?

@vincent15000 Yes, back() returns a redirect response. How do you think the browsers gets back to the previous page if not with a redirect?

back() is just shorthand for redirect()->back(), which just redirects to the previous page. However, I personally like to explicitly define a route (e.g. redirect()->route('route.name')) to avoid the potential of redirect loops.

1 like

Please or to participate in this conversation.