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

jpeterson579's avatar

How to get variable route id into my inertia/vue3 post request

Trying to assign the proper route ID to my post request but I'm lost as to why it's not working. I cannot seem to access accountId inside of my submit_new_property function. Any advice?

AccountController:

public function index($id) {
        return Inertia::render('Account', [
            'accountId' => $id,
            'accounts' => Account::paginate(100)->through(fn($account) => [
                'id' => $account->id,
                'name' => $account->name,
            ]),
        ]);
    }

Account.vue:

<script setup>
import { ref } from 'vue';
import { useForm } from '@inertiajs/inertia-vue3';
import AppLayout from '@/Layouts/AppLayout.vue';
import Welcome from '@/Components/Welcome.vue';
import DialogModal from '@/Components/DialogModal.vue';
import PrimaryButton from '@/Components/PrimaryButton.vue';
import SecondaryButton from '@/Components/SecondaryButton.vue';
import TextInput from '@/Components/TextInput.vue';
import { reactive } from 'vue';

let create_new_property = reactive({
	name: '',
});

let submit_new_property = () => {
	// Submit new property creation
	Inertia.post('/accounts/{accountId}', create_new_property);
};
</script>

<form @submit.prevent="submit_new_property" id="create_new_property">
				<label for="new_property_name">Property Name</label>
					<TextInput
						v-model="create_new_property.name"
						id="new_property_name"
						ref="textInput"
						type="text"
						class="mt-1 block w-full"
						name="new_property_name" 
						placeholder="New Property Name"
						required="required"
					/>
</form>

<script >
	import { Link } from '@inertiajs/inertia-vue3';
	import { Inertia } from '@inertiajs/inertia';

	export default {
		components: { Link },
		props: {
			accountId: String,
		    accounts: Object,
		},
	};

</script>
0 likes
5 replies
tykus's avatar
tykus
Best Answer
Level 104

It is available as a prop; which you can get using defineProps in script setup rather than in the regular script. Just remove the Options API <script> element altogether and define the props like this using the Composition API

let props = defineProps({
  accountId: String,
  accounts: Object,
});

let submit_new_property = () => {
	// Submit new property creation
	Inertia.post(`/accounts/${props.accountId}`, create_new_property);
};
jpeterson579's avatar

@tykus Thanks, that's helpful, and now I can console log out the id I need however the url it's posting to now looks like this

http://localhost:8888/accounts/%7Bprops.accountId%7D
jpeterson579's avatar

@tykus Changed to

Inertia.post('/accounts/' + props.accountId + '/property', create_new_property);

It works but is this ok?

tykus's avatar

@jpeterson579 use template string:

Inertia.post(`/accounts/${props.accountId}`, create_new_property);

Please or to participate in this conversation.