nomikz's avatar

$request is not working as expected

This is how I make request from front

editDocument() {
                this.$refs.form.validate();

                let formData = new FormData();
                formData.append('name', this.document.name);
                formData.append('document', this.document.document);
                formData.append('category', this.document.category);
                formData.append('description', this.document.description);
                formData.append('user_id', this.user.id);

                axios.put('/api/documents/' + this.document.id, formData, {
                    headers: {'Content-Type': 'multipart/form-data'},
                }).then(response => {
                    // this.$refs.form.reset();
                    // this.$router.push({name: 'all-docs'});
                });
            },

this is in api.php

Route::apiResource('documents', 'API\DocController');

this is code in the controller method

    /**
     * Update the specified resource in storage.
     *
     * @return void
     */
    public function update(Request $request, Document $document)
    {
        dd($request->all());
    }

$request->all() is showing an empty array. This is very strange.

Interestingly when I do $request->document it shows data from the database.

0 likes
4 replies
nomikz's avatar

I've edited. When I do $request->all() it shows an empty array. When I do dd($request) it correctly shows me that it is Request instance. The problem is $request doesn't contain my request parameters. I have zero idea why. $request->name is null when I dump it. I've tried to do valet restart to see if it is something with my valet. No success.

This is from the networks

-----------------------------58601862741120169421434528581
Content-Disposition: form-data; name="name"

Another new document
-----------------------------58601862741120169421434528581
Content-Disposition: form-data; name="document"; filename="sdu.svg"
Content-Type: image/svg+xml
svg
-----------------------------58601862741120169421434528581
Content-Disposition: form-data; name="category"

2
-----------------------------58601862741120169421434528581
Content-Disposition: form-data; name="description"

This is a new document. And it is about new things;
-----------------------------58601862741120169421434528581
Content-Disposition: form-data; name="user_id"

1
-----------------------------58601862741120169421434528581--
nomikz's avatar
nomikz
OP
Best Answer
Level 8

The thing is if there is a file (formData) in your request parameters, Request object can't see parameters when PUT or Patch methods is used. Workaround for this is to use post method. Kind of pity. https://github.com/laravel/framework/issues/13457

It would have been great if laravel would have thrown an error or warning because it is misleading and may take a lot of hours of debugging until someone finds out that it is php related bug.

mehdi_developer's avatar

Hi dear friends I had the same problem؛ My problem was that I was not careful when using Request Request class should be used from Illuminate\Http But I had mistakenly used Request from Illuminate\Support\Facades I hope you will be successful

1 like

Please or to participate in this conversation.