0

in a little laravel application I'm working on I'm returning data from an ajax request like this:

return response ()->json ( $subject::where('id', $subject->id)->with('division')->get(['id', 'name']));

This returned something quite like an object that have nested objects. This how my results looks when I log it to the console.

enter image description here

I want to get the name and id of the subject details returned, which in this case is History and 8. Also I want to be able to access the division array and properties of the object it has.

I do this to log the name of the subject console.log(data.name) but in returned I get:

undefined

How can I achieve this?

Nathan Siafa
  • 707
  • 4
  • 15
  • 36

3 Answers3

1

You have an array of objects, which has one property (division) - which contains another array. So you have to access the array indices

console.log(data[0].division[0].name);
tymeJV
  • 102,126
  • 13
  • 159
  • 155
1

Looking at the object structure we see that this is an array of elements, so it should be data[0].name to fetch History text

Kukic Vladimir
  • 1,012
  • 4
  • 15
  • 22
1

I'm assuming data is your entire object. Try data[0].name

yusif
  • 193
  • 9