Answer the question
In order to leave comments, you need to log in
How does Laravel return multiple objects per request?
Hello!
I make a request to the simplest API, which should return three values.
If the controller usesreturn Set::with('items')->get();
Then three collections come as saysalert();
[object object], [object object], [object object]
return Set::with('items')->get()->mapWithKeys(function ($item) { return [$item->id => $item]; });
alert();
it says that an object with collections has returned:[object object]
dd()
- Visually, the data looks and is located the same. Answer the question
In order to leave comments, you need to log in
1. Read the JSON spec
2. Stop debugging alerts in 2018
[{}, {}, {}]
and
{"0": {}, "1": {}, "2": {}}
are slightly different things
Something you misunderstand about JSON.
You receive 1 array of objects, in which there are 3 objects. This is not much different from the second situation.
To understand why, see the example: sandbox.onlinephpfunctions.com/code/342145de51348c...
$ar = [0 => 0, 1 => 1, 2 => 2];
$ar2 = [1 => 1, 0 => 0, 2 => 2];
var_dump(json_encode($ar));
var_dump(json_encode($ar2));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question