Md. Shamvil Hossain
Md. Shamvil Hossain

Reputation: 424

send Array of object via axios post in laravel vue

I am using vue and laravel. I getting an array of objects like this in console.log(array_of_obj) :

[…]
​
0: Object { id: Getter & Setter, employee_id: Getter & Setter, manual_id: Getter & Setter, … }
​
1: Object { id: Getter & Setter, employee_id: Getter & Setter, manual_id: Getter & Setter, … }
​
2: Object { id: Getter & Setter, employee_id: Getter & Setter, manual_id: Getter & Setter, … }

Each object contains my desired row data. Now I want to send it to Laravel controller via axios post request. How Can I do this? I have tried this way:

let json=JSON.stringify(array_of_obj);
let post_data={json_data:json}
 store.dispatch('employees/EmployeeListDownloadPdf', post_data).catch(err => { console.error(err) })

In my vuex section:

EmployeeListDownloadPdf(ctx, employeeList) {
                return new Promise((resolve, reject) => {
                    
                    axios({
                        url: '/api/employees/employeeListData',employeeList,
                        method: 'POST',
                        
                      })
                        //.then(response => resolve(response))
                        .then((response) => {
                            //console.log(response.data)
                            
                          });
                        //.catch(error => reject(error))
                })
            },  

But in my controller I tried with following code but getting empty array.

dd(json_decode($request->all(),true));
dd(json_decode($request,true));

I want to get all data into my controller but can not accesss them now. How Can I do this?

Upvotes: 0

Views: 2194

Answers (1)

Terry
Terry

Reputation: 66093

You're using axios() incorrectly: if you read the docs, the employeeList needs to be assigned to the data property in the object, i.e.:

axios({
    url: '/api/employees/employeeListData',
    data: employeeList,
    method: 'POST'
})

Upvotes: 1

Related Questions