4

//Vuejs2 //Laravel v7.x

I turn to you because I'm blocking, I can't find a solution. I want to recover the data in my object in my controller. In my View.vue I make an axios post

     data() {
        return {
          customer: {
             name: 'abc',
             login: 'def'
          },
          file: null
        }
    },methods: {
        submit(){
            let formData = new FormData();
            formData.append("customer", this.customer);
            formData.append("file", this.file);

            axios.post('/project/new',
                        formData, {
                            headers: {
                                "Content-Type": "multipart/form-data"
                            }
                        }).then(data => {
                        console.log(data.data);
                    });
        }
    }

I collect like that in my controller

public function postProject(Request $request)
{
    return $request->customer;  //return [Object Object]
    return $request->customer->name;  //return Trying to get property 'name' of non-object
    return $request->customer['name']; //return Illegal string offset 'name'
    return $request->file;  //return [Object Object]
}

Thx for help. have a nice day.

0

2 Answers 2

3

You can't use .append to add objects to your FormData.

Looking at https://developer.mozilla.org/en-US/docs/Web/API/FormData/append the method only accepts an USVString or Blob as the value. Everything else is casted to String.

And the string representation of a standard object in Javascript is [object Object].

You can try JSON.stringify(this.customer) to convert it to its JSON representation.

Sign up to request clarification or add additional context in comments.

2 Comments

I really want to thank you! it finally works! other quick question do I need JSON.stringify if I send files?
To send a File you have to pass an instance of the File class to FormData developer.mozilla.org/de/docs/Web/API/File
-2

Try $request->get('customer') or $request->input('customer')

Comments

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.