E
E
Evgeny Pavlov2021-06-19 10:39:34
Laravel
Evgeny Pavlov, 2021-06-19 10:39:34

How to return newly created post back to inertiajs template?

There is a record creation form. I send the form to the back using this.$inertia.METHOD. The record is created and needs to be returned back to the page with the form using inertia. redirect()->back()->with() doesn't work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Sarvarov, 2021-08-31
@megakor

No way.
Inertia is not an API, but a monolith. When you submit a form (or make an inertia request via $inertia.visit, $inertia.get, $inertia.post, etc.), then Inertia expects an InertiaResponse or RedirectResponse as a response, but an array of data.
Those. in inertia in the controller, you only redirect back through back(), or return the correct response

return inertia()->render('Some/Component', ['some' => 'data'])

There is no synchronization of the form with the server response (as we used to do in vue) when using the Inertia form helper - if an error was not created in the form and the record was not created, then inertia itself will understand this and should roll back the form through form.reset().
If you need to return something from the server, then you need to use axios or fetch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question