I am sending an array of objects to a Laravel controller via axios, and I'm looking for an easier way to iterate through the array to access the objects' data. It is something like the following:
Javascript:
var some_objects = [];
var some_object1 = new SomeObject('value1', 'value2', 'value3');
objects.push(object1);
var some_object2 = new SomeObject('value4', 'value5', 'value6');
objects.push(object2);
axios.post(endpoint, {some_objects: some_objects}).then(function(response) {console.log(response);})
The issue is accessing the objects array. It looks like this:
some_objects: [
{property1: value1, property2: value2, property3: value3},
{property1: value4, property2: value5, property3: value6}
]
Laravel:
public function endpoint(Request $request){
$objArray1 = $request->some_objects; // or $request->input('some_objects');
$some_object1 = some_objects[0]; // this correctly assigns the object.
$some_object1->property1 // RETURNS ERROR!
}
As above, trying to access any of the object's data results in "Trying to get property 'property1' of non-object"
The only way I've found to access the properties are to use
SomeObject1Property1 = $request->input('some_objects.0.property1');
But that doesn't help if I don't know how many objects there are, and if I did, it's cumbersome to handle. How can I more easily iterate and access these properties?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire