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

lyndon's avatar

A controller method is not being called from an Inertia form post

I have this template

<template>
    <dashboard-layout title="Dashboard">
        <template #header>
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
				<Link class="text-blue-600" :href="route('dashboard')">Dashboard</Link> / 
				Settings
				<label @click="submit" class="float-right text-xs bg-gray-800 rounded-lg text-white hover:bg-gray-700 px-4 py-2 cursor-pointer">SAVE CHANGES</label>
            </h2>
        </template>

		<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
			<form id="settings_form" @submit.prevent="submit">
				<h1 class="text-xl font-black mb-1">Template</h1>
				<p class="my-2">Select a template for your store</p>
				<div class="grid grid-cols-3 gap-4">
					<label for="default" class="p-4 border rounded-lg cursor-pointer">
						Default
						<input v-model="form.shop_template" id="default" value="default" type="radio" class="float-right right-0 mt-1">
					</label>
					<label for="awesome" class="p-4 border rounded-lg cursor-pointer">
						Awesome
						<input v-model="form.shop_template" id="awesome" value="awesome" type="radio" class="float-right right-0 mt-1">
					</label>
				</div>

				<h1 class="text-xl font-black mb-1 mt-6">Payments</h1>
				<p class="my-2">Choose the payment methods to be enabled on your store</p>
				<div>
					<jet-label for="cash_on_delivery">
						<div class="flex items-center">
							<input type="checkbox" v-model="form.shop_payment_options" value="cash_on_delivery" id="cash_on_delivery" class="rounded border-gray-300 text-indigo-600 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"/>
							<div class="ml-2">
								Cash on delivery
							</div>
						</div>
					</jet-label>
					<jet-label for="mobile_money">
						<div class="flex items-center">
							<input type="checkbox" v-model="form.shop_payment_options" value="mobile_money" id="mobile_money" class="rounded border-gray-300 text-indigo-600 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"/>
							<div class="ml-2">
								Mobile money
							</div>
						</div>
					</jet-label>
					<jet-label for="paypal">
						<div class="flex items-center">
							<input type="checkbox" v-model="form.shop_payment_options" value="paypal" id="paypal" class="rounded border-gray-300 text-indigo-600 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"/>
							<div class="ml-2">
								Paypal
							</div>
						</div>
					</jet-label>
					<jet-label for="card">
						<div class="flex items-center">
							<input type="checkbox" v-model="form.shop_payment_options" value="card" id="card" class="rounded border-gray-300 text-indigo-600 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"/>
							<div class="ml-2">
								Card (Visa or Mastercard)
							</div>
						</div>
					</jet-label>
				</div>

				<h1 class="text-xl font-black mb-1 mt-6">Shipping</h1>
				<p class="my-2">Select shipping methods your store provides</p>
				<div>
					<jet-label for="same_day_shipping">
						<div class="flex items-center">
							<input type="checkbox" v-model="form.shop_shipping_options" value="same_day_shipping" id="same_day_shipping" class="rounded border-gray-300 text-indigo-600 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"/>
							<div class="ml-2">
								Same day shipping
							</div>
						</div>
					</jet-label>
					<jet-label for="2_day_shipping">
						<div class="flex items-center">
							<input type="checkbox" v-model="form.shop_shipping_options" value="2_day_shipping" id="2_day_shipping" class="rounded border-gray-300 text-indigo-600 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"/>
							<div class="ml-2">
								2-day shipping
							</div>
						</div>
					</jet-label>
				</div>

				<h1 class="text-xl font-black mb-1 mt-6">Domain: <span class="font-gray-300 font-thin lowercase">{{this.$page.props.user.shop_name}}.copify.com</span></h1>
				<p class="my-2">Add your custom domain</p>
				<div class="mt-2">
					<jet-input id="shop_domain" type="text" v-model="form.shop_domain" class="mt-1" />
				</div>
				<jet-button class="mt-4 float-right">
					Save Changes
				</jet-button>
			</form>
		</div>
    </dashboard-layout>
</template>

<script>
    import DashboardLayout from '@/Layouts/DashboardLayout.vue'
    import { Head, Link } from '@inertiajs/inertia-vue3'
    import JetInput from '@/Jetstream/Input.vue'
    import JetCheckbox from '@/Jetstream/Checkbox.vue'
    import JetLabel from '@/Jetstream/Label.vue'
    import JetButton from '@/Jetstream/Button.vue'

    export default {
        components: {
            DashboardLayout,
            Head,
            Link,
            JetInput,
            JetCheckbox,
            JetLabel,
			JetButton
        },

        data() {
            return {
                form: this.$inertia.form({
					_method: 'PATCH',
                    shop_template: '',
                    shop_payment_options: [],
                    shop_shipping_options: [],
					shop_domain: ''
                })
            }
        },

        methods: {
            submit() {
                this.form.post(this.route('shops.update', 1))
            }
        }
    }
</script>

And then, this controller...

public function update(Request $request, Shop $shop)
    {
        return $request;
        // dd($request);
    }

Route also defined as...

Route::resource('shops', ShopController::class);

The function in the template is being called because when I console.log() something, it shows in the browser console. but the controller method, for some reason doesn't respond.

0 likes
25 replies
tykus's avatar

Try replacing this.form.post with this.form.put or this.form.patch rather than spoofing the Request method in the object.

Otherwise, there is nothing obvious in the code you share (assuming the route helper resolves to the correct URL)

lyndon's avatar

this.form.put and this.form.patch don't work either

tykus's avatar

What is the URL - check your devtools.

tykus's avatar

Looks okay; and that is showing a PUT Request in the Network tab

tykus's avatar

Response code is in success (2xx) range or 3xx??

lyndon's avatar

I am so not sure since I have no code in the controller method, just a dd() or return statement

tykus's avatar

I supposed the redirect might be coming from a middleware rather than the Controller action.

EDIT Regarding earlier comment about the _method spoofing approach, see this comment on Github

lyndon's avatar

The only middleware I have is...

Route::middleware(['auth:sanctum', 'verified'])->group(function () {
    ...

    Route::resource('shops', ShopController::class);
    ...
});

And all other routes work just fine

lyndon's avatar

I just tried taking the route outside of the middleware and it didn't change anything.

tykus's avatar

Do you have a Shop model with id of 1?

I would expect a 404 response, but perhaps it is converted to a 3xx? You could remove the App\Models\Shop typehint in the Controller action to test this theory?

public function update(Request $request, $shop)
lyndon's avatar

About removing the type-hint... I tried that also, didn't change anything

tykus's avatar

Somewhere (perhaps) an exception is being converted to a 3xx (before Inertia converts to a 303) - in the app/Exceptions/Handler.php class, can you dd in the `render method so there is some visibility of the possible Exception?

tykus's avatar
tykus
Best Answer
Level 104

Create one in app/Exceptions/Handler.php:

    public function render($request, Throwable $e)
    {
        dd($e);
    }
1 like
lyndon's avatar

I have figured it out now... silly me๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ

here is the error message from the exception you mentioned (Thank you for the tip btw)... https://paste.pics/61d8e6b00d355c72abfce0bbf491b596

So I am using a ShopRequest as Request, which includes some validation rules with required fields. And guess what? the form I am submitting is an update form that does not include these fields.

I still can't believe I have spent days on this๐Ÿ˜…

Thank you so much, @tykus for your time and patience. I really appreciate it.

tykus's avatar

This is what was posted earlier:

public function update(Request $request, Shop $shop)

Had you properly shown the ShopRequest instead, this would have been a much shorter thread @lyndon ๐Ÿ˜†

1 like

Please or to participate in this conversation.