1

I've successful uploaded files with vue.js 1.0 like this (post request):

store () {
            this.storingMessage = true;
            var form = new FormData();
            form.append('subject', this.message.subject);
            form.append('message', this.message.message);
            form.append('topic_id', this.message.topic_id);

            for(var key in this.message.attachment) {
                form.append('attachment[' + key + ']', this.message.attachment[key]);
            }

            MessageService.store(this, form);
        }

But when I try to update files like this (put request):

update () {
            this.updatingMessage = true;
            var form = new FormData();
            form.append('subject', this.message.subject);
            form.append('message', this.message.message);
            form.append('slug', this.message.slug);

            for(var key in this.message.attachment) {
                form.append('attachment[' + key + ']', this.message.attachment[key]);
            }

            MessageService.update(this, form);
        }

And in my controller I dd($request->all()) result is [].

So why is it not working with put????!?!?!?

Services:

store (context, form) {
        return Vue.http.post('/api/message', form)
            .then(({data}) => {
                context.success();
            }, (error) => {
                context.error(error.data);
            });
    },

    update (context, form) {
        return Vue.http.put('/api/message/' + form.get('slug'), form)
            .then(({data}) => {
                context.success();
            }, (error) => {
                context.error(error.data);
            });
    }

1 Answer 1

1

According to a Laracast Discussion it would seem there is a problem when using the put method and a formData object a way to avoid this is with method spoofing.

Try this instead, in your update method

let data = { _method : 'PATCH' , form : form}

return Vue.http.post('/api/message/' + form.get('slug'), data)
    .then(({data}) => {
        context.success();
    }, (error) => {
        context.error(error.data);
    });

your data in your controller will be available as form in this case

$request->form
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for helping. I'm using form data so I had to do it like this: form.append('_method', 'PUT');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.