Have you gone over: https://inertiajs.com/forms
laravel inertia post data
I'm looking for a way to post data in Inertia but failing so far to parse ID param.
This is my current controller.
Route::get('/post/edit/{id}', [PostController::class, 'postEdit']) ->name('postEdit');
Because ID is in the middle, I'm not sure how to post it. I added it in props in Vue, and the problem is here in submitting it. Not sure how to add properly id at the end
methods: { submit() { this.$inertia.post('/post/edit/' + data.id , this.form)
},
},
Could you copy and paste your Vue component? I don't know where your data.id is coming from. If it's defined either in props or data like so:
<template>
</template>
<script>
export default {
// either here
props: {
id: Number
},
// or here
data: {
return {
id: 1
}
}
}
</script>
Then you should access id in your method with this.id:
submit () {
this.$inertia.post(`/post/edit/${this.id}`, this.form);
}
If you've defined your form as type $inertia.form, then you could also simply do
submit () {
this.form.post(`/post/edit/${this.id}`);
}
On top:
While I was wondering why you didn't use standard naming like /posts/{id}/edit I saw your route configuration. And it looks like you're trying to POST to a route that is defined as GET. So if you want to update a post, it actually should be:
Route::put('posts/{post}', [PostController::class, 'update'])->name('posts.update');
Then your method in your Vue should also be put and not post of course:
submit () {
this.form.put(`/posts/${this.id}`);
}
Please or to participate in this conversation.