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

Randy_Johnson's avatar

InertiaJS Update image isn't working

I am updating an item in InertiaJS, but when adding an image, the image file isn't being sent to the controller.

<div className="col col-span-2">
                                <InputLabel>Image</InputLabel>
                                <input id="image" name="image" onChange={e => setData('image', e.target.files[0])} className="block w-full text-sm text-gray-900 border border-gray-300 rounded-lg cursor-pointer bg-gray-50 dark:text-gray-400 focus:outline-none dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400" aria-describedby="file_input_help" type="file" required />
                                <p className="mt-1 text-sm text-gray-500 dark:text-gray-300" id="file_input_help">SVG, PNG, JPG or GIF (Ensure file size is low for fast loading times).</p>
                                {processing && (
                                    <div value={processing.percentage} max="100">
                                        {processing.percentage}%
                                    </div>
                                )}
                            </div>
    const { data, setData, put, processing, errors, reset } = useForm({
        id: '',
        collection_id: '',
        category_id: '',
        name: '',
        description: '',
        image: '',
        price: '',
        amount: '',
    });
    const submit = (e) => {
        e.preventDefault();
        
        put('/admin/product/{product}', data);
        setShowUpdate(false);

        let index = tuples.findIndex(obj => obj.id === tuple.id);

        if (index !== -1) {
            let updateTuples = [
                ...tuples.slice(0, index),
                data,
                ...tuples.slice(index + 1)
            ];
            setTuples(updateTuples);
        }
    };
    public function update(Request $request)
    {
        dd($request);
        $path = ImageController::store($request->file('image'), 'collection');

        $data = Product::find($request->id);
        $data->collection_id = $request->collection_id;
        $data->category_id = $request->category_id;
        $data->name = $request->name;
        $data->description = $request->description;
        $data->image = $request->image;
        $data->price = $request->price;
        $data->amount = $request->amount;
        $data->image = $path;
        $data->save();
    }
PUT|PATCH       admin/product/{product} ......................................... product.update › ProductController@update
0 likes
7 replies
tykus's avatar
ImageController::store($request->file('image'), 'collection');

ImageController???

Randy_Johnson's avatar

@tykus Yeah, well I didn't go about it in a smart way, and its an old project.

Basically when I send using Create - the image has been sent across using post, but for some reason, when using put or patch, the $request File is empty.

Randy_Johnson's avatar

@tykus

Create

+files: Symfony\Component\HttpFoundation\FileBag {#48 ▼ #parameters: array:1 [▼ "image" => Symfony\Component\HttpFoundation\File\UploadedFile {#33 ▶} ] }

Put

+files: Symfony\Component\HttpFoundation\FileBag {#48 ▼ #parameters: [] }

tykus's avatar

@Randy_Johnson can you show the markup for the form(s)? The update form might be missing the enctype attribute???

EDIT forced FormData for Inertia

Randy_Johnson's avatar

I thought about this, then couldn't figure out why it would work on Create

Update

<form onSubmit={submit}>
                        <div className="flex items-start justify-between border-b rounded-t dark:border-gray-600 p-4">
                            <h3 className="text-xl font-semibold text-gray-900 dark:text-white">
                                Update Product
                            </h3>
                            <button onClick={() => setShowUpdateModal(false)} type="button" className="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white" data-modal-hide="defaultModal">
                                <svg className="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fillRule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clipRule="evenodd"></path></svg>
                                <span className="sr-only">Close modal</span>
                            </button>
                        </div>

Create

<form onSubmit={submit}>
                        <div className="flex items-start justify-between border-b rounded-t dark:border-gray-600 p-4">
                            <h3 className="text-xl font-semibold text-gray-900 dark:text-white">
                                Create Product
                            </h3>
                            <button onClick={() => setShowCreate(false)} type="button" className="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white" data-modal-hide="defaultModal">
                                <svg className="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fillRule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clipRule="evenodd"></path></svg>
                                <span className="sr-only">Close modal</span>
                            </button>
                        </div>

Please or to participate in this conversation.