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" >Username <span >*</span></label>
<input type="text" name="username" id="username" v-model="form.username" />
<div >
<button type="submit" >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.
CodePudding user response:
Try Inertia.put()
instead, since you sending it via PUT
request. Also I think you can also use form.put()
so that you don't to type the attributes again. just make sure that the const form = useForm()
.
CodePudding user response:
You could also use the Inertia.form()
.
<script setup>
import { Inertia } from '@inertiajs/inertia';
const props = defineProps({
user: Object,
avatar: String,
});
const form = Inertia.form({
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() {
form.put(`/dashboard/users/${props.user.id}`, form)
}
</script>