To receive only the newly created record after submitting a form using Inertia useForm hook, you can modify your Laravel controller method to return the newly created record as a JSON response. Here's an example:
public function store(Request $request)
{
// validate the form data
$validatedData = $request->validate([
'name' => 'required',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:6',
]);
// create a new user record
$user = User::create($validatedData);
// return the newly created record as a JSON response
return response()->json([
'user' => $user,
]);
}
Then, in your Vue component, you can handle the response and add the newly created record to your users list. Here's an example:
import { useForm } from '@inertiajs/inertia-vue3';
export default {
setup() {
const { data, post, processing } = useForm({
name: '',
email: '',
password: '',
});
const users = ref([]);
const handleSubmit = async () => {
const { data: { user } } = await post('/users', data);
// add the newly created user to the users list
users.value.push(user);
// reset the form data
data.name = '';
data.email = '';
data.password = '';
};
return {
data,
processing,
handleSubmit,
users,
};
},
};
Similarly, you can modify your update and destroy methods to return the updated or deleted record as a JSON response, and handle the response in your Vue component to update or remove the record from your users list.