I`m new to laravel and i try to do a crud tutorial but donesn t work the update method.
This is the controller
public function update(UserRequest $request, User $user)
{
$avatar = $user->avatar;
if(Request::file('avatar')){
Storage::delete('public/'. $user->avatar);
$avatar = Request::file('avatar')->store('users', 'public');
}
$validated = $request->validate([
'username' => ['required', 'unique:users', 'max:255'],
'name' => ['required', 'max:255'],
'lastname' => ['required', 'max:255'],
'email' => ['required', 'email', 'unique:users', 'max:50'],
'role_id' => ['required'],
'avatar' => $avatar,
]);
$user->update($validated);
return Redirect::route('users.index');
}
This is the view
const props = defineProps({
user: Object,
avatar: String,
});
const form = useForm({
username: props.user.username,
name: props.user.name,
lastname: props.user.lastname,
email: props.user.email,
role_id: props.user.role_id,
avatar: null,
});
function updateUser() {
Inertia.post(`/dashboard/users/${props.user.id}`, {
_method: 'put',
username:form.username,
name:form.name,
lastname:form.lastname,
email:form.email,
role_id:form.role_id,
avatar:form.avatar
});
}
</script>
<form @submit.prevent="updateUser">
<label for="username" class="block text-sm font-medium text-gray-700">Username <span class="text-red-600">*</span></label>
<input type="text" name="username" id="username" v-model="form.username" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm" />
<div class="bg-gray-50 px-4 py-3 text-right sm:px-6">
<button type="submit" class="inline-flex justify-center rounded-md border border-transparent bg-green-600 py-2 px-4 text-sm font-medium text-white shadow-sm hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2">Save</button>
</div>
</form>
When i click save button nothing is happening. In the consol i don t have any error .
I try to modify the controller.
2
Answers
Try
Inertia.put()
instead, since you sending it viaPUT
request. Also I think you can also useform.put()
so that you don’t to type the attributes again. just make sure that theconst form = useForm()
.You could also use the
Inertia.form()
.