Retrieve entry data from an Entries field in a Matrix Block I'm using a Matrix field for menu section blocks that each have a name, section and menu items. The menu items for that menu section are assigned using an Entries fieldtype that displays entries from food and beverage entries. Here's a screenshot of the field setup in case that helps explain it better:



What I'm trying to do is generate a menu endpoint that has the individual sections and within each section display the item data. The json response I'm looking for should be similar to this structure:


        "menu": {
          "id": 1,
          "sections": [
            {
              "id": 1,
              "name": "Heavy",
              "description": "All our heavy draft beers.",
              "items": [
                {
                  "id": 2,
                  "name": "Samuel Adams Utopias",
                },
                {
                  "id": 3,
                  "name": "Samuel Adams Utopias",
                },
              ]
            },
            {
              "id": 2,
              "name": "Light",
              "description": "All our light draft beers.",
              "items": [
                {
                  "id": 2,
                  "name": "Samuel Adams Utopias",
                },
                {
                  "id": 3,
                  "name": "Samuel Adams Utopias",
                },
              ]
            },
          ]
        }


I can accomplish the section structure just fine, but I can't get the data from the sectionItems field which is an Entries type. This is what I have minus the items call.


        'api/v1/locations/&lt;entryId:\d+&gt;/menu.json' =&gt; function($entryId) {
          return [
            'elementType' =&gt; 'Entry',
            'criteria' =&gt; [
              'id' =&gt; $entryId,
              'type' =&gt; 'location'
            ],
            'first' =&gt; true,
            'transformer' =&gt; function(EntryModel $entry) {
              foreach ($entry-&gt;menu as $block) {
                $menuBlocks[] = [
                  'id' =&gt; $block-&gt;id,
                  'menu_id' =&gt; $block-&gt;fieldId,
                  'name' =&gt; $block-&gt;sectionName,
                  'description' =&gt; $block-&gt;sectionDescription,
                  'items' =&gt; $block-&gt;sectionItems,
                ];
              }

              return [
                'title' =&gt; $entry-&gt;title,
                'menu' =&gt; [
                  'sections' =&gt; $menuBlocks
                ]
              ];
            },
          ];
        },


Any help would be greatly appreciated!
You’re off to a great start here, in the way that you are first assembling the $menuBlocks array, and then finally returning the main array with the title and menu keys. For the section items, you just need to take the exact same approach:

'transformer' =&gt; function(EntryModel $entry) {
  // Note: you should also make sure you define the $menuBlocks
  // array, to avoid a PHP error in case there are 0 blocks.
  $menuBlocks = [];

  foreach ($entry-&gt;menu as $block) {
    // Now start by defining an $items array:
    $items = [];

    // And loop through your sectionItems field just like
    // you are doing with the menu field.
    // (Note - you can’t name the individual item entries "$entry",
    // because that would override the other $entry variable.)
    foreach ($block-&gt;sectionItems as $itemEntry) {
      $items[] = [
        'title' =&gt; $itemEntry-&gt;title,
        // ...
      ];
    }

    $menuBlocks[] = [
      'id' =&gt; $block-&gt;id,
      'menu_id' =&gt; $block-&gt;fieldId,
      'name' =&gt; $block-&gt;sectionName,
      'description' =&gt; $block-&gt;sectionDescription,

      // Now insert that whole $items array here:
      'items' =&gt; $items,
    ];
  }

  return [
    'title' =&gt; $entry-&gt;title,
    'menu' =&gt; [
      'sections' =&gt; $menuBlocks
    ]
  ];
},