0

I'm trying to output a table within a matrix of a SuperTable for element API.

I fail to output $projectList, I keep getting the error 'Invalid argument supplied for foreach()' or 'Trying to get property 'bizProjectItem' of non-object'

'transformer' => function(Entry $entry) {
  $bodyRows = [];

  foreach ($entry->bizTable as $row) {
    $bodyprojects = [];

    foreach ($row->bizprojectMatrix as $block) {
      switch ($block->type->handle) {
        case 'bizprojectBlock':
          $projectList = [];

          $bodyprojects[] = [
            'projectTitle' => $block->bizproject1,
            'projectDetails' => $block->bizproject2,
            'lists' => $projectList
          ];

          // Fail to output anything
          foreach ($block->bizProject as $project) {
            $projectList[] = [
              'projectList' => $project->bizProjectItem
            ];
          };

          break;
      }
    }

    $bodyRows[] = [
      'title' => $row->bizTitle,
      'projects' => $bodyprojects
    ];
  }

  return [
    'type' => 'projects',
    'details' => $bodyRows
  ];
},

The error not shown anymore after I update it as below, but it fails to output the details.

if (is_array($block->bizProject) || is_object($block->bizProject)){
  foreach ($block->bizProject as $project) {
    if(!empty($project->bizProjectItem)) {
      $projectList[] = [
        'projectList' => $project->bizProjectItem
      ];
    };
  };
};

Here is my front-end,

{% for bizRow in entry.bizTable %}
    {% for bizProjectItem in bizRow.bizProjectMatrix.type('bizProjectBlock') %}
        {% for bizProjectRow in bizProjectItem.bizProject %}
            {{ bizProjectRow.bizProjectItem }}
        {% endfor %}
    {% endfor %}
{% endfor %}

Here is my latest update, I manage to get the data by using this method, but I will only get the first data using this method. How my loop should look like if I want to get all the data?

'lists' => $block->bizProject[0]['bizProjectItem']

Your Answer

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

Browse other questions tagged or ask your own question.