Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AlexJ123's avatar

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)

},

},

0 likes
13 replies
AlexJ123's avatar

Yes, already tried and I can't figure it out. The main problem is that parameter at the end ID, usually it was like this. /post/{id}/edit/, but thought this will be easier to figure out, but still no

jlrdw's avatar

When posting it's in the request object it's not a parameter, a parameter is normally a get request.

AlexJ123's avatar

As example, where I'm posting it looks like this in the router

return redirect(route('main_page', $post->id));

On my old laravel 6 post request looked like this

{{ route('postEdit, $category->id) }}" and it worked that way

So not sure how to do it here

AlexJ123's avatar

Thank you for pointing, but still, could you figure out this.

this.$inertia.post('/post/edit/' + data.id , this.form)

How to make this properly?

PaulMaxOS's avatar
Level 8

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}`);
}
2 likes
AlexJ123's avatar

Thank you, now when I made it like that at the console I see the end of the URL is now /post/edit/undefined

I have tried it both ways as you made. to put both as props and data return

PaulMaxOS's avatar

Could you copy and paste your whole vue component? On top I had a mistake in the last code block. I only updated the form method but forgot to update the path. I just updated that.

Furthermore: You shouldn't have id in props and data. It's either in props or in data. And if it is in props make sure you're passing the ID in your

Inertia::render('YourPage', [ 'id' => $id ]);
AlexJ123's avatar

The problem here is that I have page AllPosts.vue, and there ID is not present, it's only present in another function return for posting/editing. Because before I was doing this

{{ route('postEdit, $category->id) }}"

ID was never present on the page I was being on, and never loaded in controller for that page

PaulMaxOS's avatar

Sorry, but I don't completely get what you mean. Where do you implement your form, i mean on which page / component? If your component doesn't know the ID of the post you're trying to update then you simply can't use the ID in this component. Of course you could extract the ID out of the post itself like so this.post.id. But this requires the post to be available in the component.

So regarding your {{ route('postEdit', $category->id) }}: Again I don't really understand why you would edit a post based of the ID of a category. But anyway: you can use the same route() function in Inertia / Vue with ziggy: https://github.com/tighten/ziggy

On top, please don't mix up terms with Edit and Update:

EDIT is used as a name for a GET request for displaying a form which updates an entity.

UPDATE is used for a PUT or PATCH request that actually updates the entity. So the form of your EDIT will perform a put request to UPDATE:

https://laravel.com/docs/8.x/controllers#actions-handled-by-resource-controller

So well, if you could just share your routes, controller and vue components, things would be a lot clearer and it would be a lot easier to help you.

jlrdw's avatar
this.$inertia.post('/post/edit/' + data.id   // is get request

A post goes like: quote

    return {
      form: {
        first_name: null,
        last_name: null,
        email: null,
      },

unquote

Just example from their site.

igmacedo's avatar

How much bureaucracy for something so simple and easy to understand.

The problem here is that you were trying to access the ID outside the defined scope.

It would have worked perfectly if you had done it like this:

<script setup>
import { useForm } from '@inertiajs/inertia-vue3';

const props = defineProps({
id: Number
});
const form = useForm({
id: props.id
});
const submit = () => {
form.post('/post/edit/' + props.id);
};
</script>

I can't believe nobody managed to solve this last year.

1 like

Please or to participate in this conversation.