0

I'm getting matrix block content from an entry type which works fine for the most part. It's probably clearer to just paste in the code:

Current element api code:


$relationsData = [];    
if($entry->relatedContent){
    $index = -1;
    foreach ($entry->relatedContent->all() as $block) {
        switch ($block->type->handle) {
            case 'relateContent':
                $index++;
                $relatedFeaturedEntries =  $block->featured->first();
                $relatedSubEntries =  $block->related->first();
                $relationsData[$index] = [
                    'title' => $relatedFeaturedEntries->title,
                    'url' => $relatedFeaturedEntries->url
                ];
                $relationsData[$index]['subItems'] = [];
                $relationsData[$index]['subItems'][] = [
                    'title' => $relatedSubEntries->title,
                    'url' => $relatedSubEntries->url
                ];
            break;
        }
    }
}

Which outputs:

      "relations": [
        {
          "title": "Testing workflow",
          "url": "http://localhost/blah",
          "subItems": [
            {
              "title": "Help & Guidance",
              "url": "http://localhost/help-guidance"
            }
          ]
        },
        {
          "title": "Taking part in the blah",
          "url": "http://localhost/blah",
          "subItems": [
            {
              "title": "blah timeline",
              "url": "http://localhost/blah"
            }
          ]
        },
        {
          "title": "Testing workflow",
          "url": "http://localhost/blah",
          "subItems": [
            {
              "title": "Completing on paper",
              "url": "http://localhost/blah"
            }
          ]
        }
      ],

There should be more subItems in each subItems array but I am only getting the first because I'm fetching them using $relatedSubEntries = $block->related->first(); But nothing else works and just errors i.e. all() or removing first()

  • "But nothing else works and just errors" - what are the errors? – Brad Bell May 22 '19 at 18:10
  • Trying to get property of non-object – Matt Simon May 23 '19 at 8:14
0

Figured it out in the end. As an array is being returned I just needed a for loop to get inside it.

Solution...

                    if($entry->relatedContent){
                        $index = -1;
                        foreach ($entry->relatedContent->all() as $block) {
                            switch ($block->type->handle) {
                                case 'relateContent':
                                    $index++;
                                    $relatedFeaturedEntries =  $block->featured->first();
                                    $relationsData[$index] = [
                                        'title' => $relatedFeaturedEntries->title,
                                        'url' => $relatedFeaturedEntries->url
                                    ];

                                    $relationsData[$index]['subItems'] = [];
                                    $relatedSubEntries =  $block->related->all();
                                    foreach ($relatedSubEntries as $relatedSubEntry) {
                                        $relationsData[$index]['subItems'][] = [
                                            'title' => $relatedSubEntry->title,
                                            'url' => $relatedSubEntry->url
                                        ];
                                    }
                                break;
                            }
                        }
                    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.