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

Randy_Johnson's avatar

InertiaJS delete route not working

I tried a thousand different ways, even checking the docs but I cannot get it to work.

Object { id: 1, cart_id: 1, product_id: 2, amount: 4, created_at: "2023-02-03T09:44:11.000000Z", updated_at: "2023-02-03T10:48:40.000000Z" }
const { data, setData, destroy, processing, errors, reset } = useForm({
        id: '',
    });
destroy(route('item.destroy', data.id), {preserveScroll: true});
    public function destroy($id)
    {
        dd($id);
    }
0 likes
5 replies
Sinnbeck's avatar

data.idis an empty string? You never give it a value ?

And what is that object in the first code block ?

1 like
tykus's avatar

You don't need useForm in this particular case; you have no payload to manage. You can use the router directly:

import { router } from '@inertiajs/vue3'

router.delete(route('item.destroy', data.id), {preserveScroll: true})
2 likes
Randy_Johnson's avatar

I got it working like this, I tried every other way upon til this point with no success. Tykus, apologies, I should mention I am using reactjs.

Take notice of the delete : destroy in the useForm.

    const { data, setData, delete : destroy, processing, errors, reset } = useForm({
        id: '',
    });

    function handleRemove(product_id) {
        let find = items.find(object => object.product_id === product_id);
        console.log(find)
        data.id = find.id;
        console.log(data.id);
        destroy(route('item.destroy', data.id), {preserveScroll: true});

        let arr = [...items];
        arr.splice(find, 1);
        setItems(arr);
    }
joseLorenzini's avatar

Hi Randy thats pretty cool , i was struggling with the delete method and i finally made it like that :

  function destroyPost(e,postId){

        e.preventDefault();

      router.delete(route('posts.destroy' , postId),{

        onBefore: () => {
            return window.confirm('Are you sure you want to delete this post?');
        },
        onSuccess: () => {
            alert('Post deleted successfully!');
        },
        onError: () => {
            alert('Failed to delete the post.');
        },

      });

    }

Please or to participate in this conversation.