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

alex32's avatar
Level 2

Inertia | return back() with props?

I've a page to Edit the psw (a GET request) , and an Update psw (PUT request) from the same page. I need something like: return back->with( props) to return server-data. Laravel doc gives: return back()->withInput(); but I don't need to return browser-data [1]

If I use Inertia::render() I can return server-data, but the url will change, and if the user refresh the page (on a PUT request) they get 405 error: 405 (Method Not Allowed)

How do I fix this? It seems the only option is to save my server-data to a session(var) before I use return back() ? Thanks

Routes for Edit & Update psw:


Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');  // Edit GET
Route::put('password', [PasswordController::class, 'update'])->name('password.update');  // Update PUT

Response for Edit & Update


return Inertia::render('Profile/Edit', [ 
            'status' => session('status') ]);  // Edit
             
return Inertia::render('Profile/Edit', [ 
            'status' => session('status'),  
             're_array' => $re_array ]);  // Update


[1] https://laravel.com/docs/11.x/responses

[2] https://inertiajs.com/redirects

0 likes
6 replies
ShahedPHP's avatar

So you’d like the form input to have the previous value entered when there’s an error?

alex32's avatar
Level 2

Thanks, but preserving the state is not what I want. I want to keep the original page URL, and to return server-data to the browser. If the original URL changes, then a page reload will trigger a 405 (because it'll send a PUT request on a Route using GET). One way to fix this would be to use ``` return back()->with (props) `` but this function doesn't exit (yet).

Breeze-React uses return back() and they return server-data with session(var). Is this the only way out? Thanks

Sinnbeck's avatar

@alex32 If you return back() I dont see why the url would change? It should just redirect to the page you were on.. And the data you want to load you need to load in the page you redirect to. If you need to pass some data (like a success message) you need to add it with ->with('success', 'Some data') and then get it in the controller of the page you are returning to, using Session::pull('success')

Replace success with any key you want.

This is also how errors are returned to the view. But they are merged into the data inside the middleware class. Find the Middleware::resolveValidationErrors() method to see how that works.

alex32's avatar
Level 2

I tried return back()-with() but didn't work, so I kept return Inertia::render() and added a Middleware to the Route::put() . That will catch the error if the user reload the page.

martinbean's avatar

@alex32 What is the actual problem you’re trying to solve here? As I’m struggling to understand.

Normally you have an edit method that returns a form in an Inertia page, and then an update method that does the actual handling of form submissions. In the update method, you redirect to where you want the user to end up if the submission was successful. If validation fails, then errors will be sent back to Inertia so you can display them on your form page.

Your controller actions should look like this:

class ProfileController extends Controller
{
    public function edit()
    {
        return Inertia::render('Profile/Edit');
    }

    public function update(UpdateProfileRequest $request)
    {
        // Update profile model here...

        return redirect()->to('profile/edit')->with('success', 'Profile updated.');
    }
}

You should not be returning an Inertia response from your update action, otherwise that will cause issues with routing, and undesirable behaviour such as users being asked if they want to re-submit the form if they tried to refresh the page.

Please or to participate in this conversation.