0

I'm using Laravel 5.3, and I'm having trouble submitting arrays of objects to my controller. Is it possible? And if not, is there a workaround so that I can submit multiple objects at once?

Example request:

let req = {
    'data[]': [
        { id: 1, name: 'test1' },
        { id: 2, name: 'test2' }
    ]
};

Then, to test, I simply returned the contents of the request:

public function testArray(Request $request) {
    return response()->json($request->all());
}

And got this result:

data: [
    "[object Object]",
    "[object Object]"
]
6
  • How are you sending the data in the request? I mean.. how do you construct your payload? Commented May 20, 2019 at 15:37
  • @HCK I'm sending it via jquery with the exact same object I put on the question. Arrays of primitives go through just fine, but arrays of objects fail Commented May 20, 2019 at 16:09
  • Are you using the header Content-type: application/json? Commented May 20, 2019 at 16:25
  • No, but where would I add the header? In the request or the response? Commented May 20, 2019 at 17:44
  • In the request. This headers tells Laravel that the incoming data is in that format (json). Additionally, you can also add another one telling the format that you expect: Accept: application/json Commented May 20, 2019 at 17:45

3 Answers 3

1

In your js

let req = [
    { id: 1, name: 'test1' },
    { id: 2, name: 'test2' }
];

var baseurl = window.location.protocol + "//" + window.location.host;
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
$.ajax({
    url: baseurl + "/test-data",
    type: 'post',
    data:{
        req:req
    },
    cache: false,
    success: function(response) {
        console.log(response);
    }
});

In controller:

public function testArray(Request $request) {
     $post = $request->all();

     return response()->json($post);
}

In route file: (web.php)

Here I used DemoController you can replace with your controller name

Route::post('/test-data', 'DemoController@testArray');

Result:

req: (2) […]
​​
0: Object { id: "1", name: "test1" }
​​
1: Object { id: "2", name: "test2" }
​​
length: 2
Sign up to request clarification or add additional context in comments.

5 Comments

I simulated your request with ajax, like this: $.post({ url: 'my url', headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': 'my token' }, data: [ { id: 1, name: 'test1' }, { id: 2, name: 'test2' } ] }) .always(res => console.log(res)); The php code was the same as you put in your answer, and my response was an empty array.
Tried it, got empty array as response
Please copy paste my code as it is. Just change your controller name only
My mistake was adding the Content-type header like the first answer told me, without it your code works just fine. Thanks!
0

In your case, result is an array of objects, result[0] is the first object. To access the id for example, you would use result[0]['id']. You can use a for loop for example to see real data.

Also I think u can use json_encode.

Hope it helps!

3 Comments

Nope, I checked it on console and my result is literally an array of strings "[object Object]". Using json_encode on the controller also didn't help, because my data array contained only strings "[object Object]" there too.
u cant check that in console... use dd($results) in your backend function
Yes I can, using console.log on an object will show its whole structure. And an answer below told me to use dd($results) in the controller and it didn't work either
0

return is often having issues when displaying objects/arrays. Try simply:

dd($request->all())

instead of return, you'll be surprised :)

1 Comment

Got an empty array as response

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.