title: Cannot use object of type App\Exports\OffetTableExport as array - Laravel Excel
body: <p>I am working on multiple sheets on laravel excel. And, I am exporting data on two sheets using two export. But, half of the data to pushed on 2nd sheets will be generated in 1st export. So I am having difficulties in passing this data from 1st expor to second sheets. This could be further clarified with the code given below:</p>
  <p><strong>mainExport</strong></p>
  <pre><code>&lt;?php
  
  namespace App\Exports;
  
  use Maatwebsite\Excel\Concerns\WithMultipleSheets;
  
  class FloorPlanExport implements WithMultipleSheets
  {
      protected $property, $mapped_columns;
  
      public function __construct($property, $mapped_columns)
      {
          $this-&gt;property = $property;
          $this-&gt;mapped_columns = $mapped_columns;
      }
  
      /**
       * @return array
       */
      public function sheets(): array
      {
          $offset = new OffsetSummaryExport($this-&gt;property);
          $resp_data = new OffetTableExport($this-&gt;property, $this-&gt;mapped_columns);
          $offset_summary = $offset + $resp_data['summary'];
  
          $sheets = [
              $resp_data['table'],
              $offset_summary
          ];
          return $sheets;
      }
  
  
  }
  </code></pre>
  <p><strong>exportRelatedToFirstSheet</strong></p>
  <pre><code>&lt;?php
  
  namespace App\Exports;
  
  use App\Helpers\SanitizeData;
  use App\Models\FloorPlan;
  use Maatwebsite\Excel\Concerns\FromArray;
  
  class OffetTableExport implements FromArray
  {
      protected $property, $mapped_columns;
  
      public function __construct($property, $mapped_columns)
      {
          $this-&gt;property = $property;
          $this-&gt;mapped_columns = $mapped_columns;
      }
  
      public function array(): array
      {
          $floorPlans = FloorPlan::with(['unitTypeDetail'])
              -&gt;where('property_id',$this-&gt;property-&gt;id)
              -&gt;get();
  
          $items = [];
          $underCountArr = $overCountArr = [];
          $underChargeArr = $overChargeArr = [];
          foreach($floorPlans as $fk =&gt; $fp){
              $temp_items = [];
              if($fk === 0 ){
                  if(in_array('pms_property',$this-&gt;mapped_columns)){
                      $temp_items['PMS Property'] = 'PMS Property';
                  }
                  $temp_items['PMS Unit Type'] = 'PMS Unit Type';
  
                  if(in_array('description',$this-&gt;mapped_columns)){
                      $temp_items['Description'] = 'Description';
                  }
                  if(in_array('beds',$this-&gt;mapped_columns)){
                      $temp_items['Beds'] = 'Beds';
                  }
                  if(in_array('baths',$this-&gt;mapped_columns)){
                      $temp_items['Baths'] = 'Baths';
                  }
                  $temp_items['Sqft'] = 'Sqft';
                  $temp_items['Unit Count'] = 'Unit Count';
  
                  $temp_items['Unit Type'] = 'Unit Type';
                  $temp_items['Base'] = 'Base';
                  $temp_items['Affordable'] = 'Affordable';
                  $temp_items['SqFtDiff'] = 'SqFtDiff';
  
                  if(in_array('pricing_offset',$this-&gt;mapped_columns)){
                      $temp_items['Current Offset ($)'] = 'Current Offset ($)';
                  }
  
                  $temp_items['RPSF ($)'] = 'RPSF ($)';
                  $temp_items['Recommended Offset ($)'] = 'Recommended Offset ($)';
                  $items[] = $temp_items;
                  $temp_items = [];
              }
  
              if($fp-&gt;affordable == 1){
                  $sqftDiff = '';
                  $rpsf = '';
                  $offset = '';
              }else{
                  if($fp-&gt;unitTypeDetail-&gt;base_sqft == null || $fp-&gt;sqft == null){
                      $sqftDiff = '';
                  }else{
                      $sqftDiff = $fp-&gt;sqft - $fp-&gt;unitTypeDetail-&gt;base_sqft;
                  }
  
                  if($this-&gt;property-&gt;rate  === null || $sqftDiff === '' || $fp-&gt;unitTypeDetail-&gt;base_sqft === null){
                      $offset = '';
  
                  }else{
                      $brpsf =  ($fp-&gt;unitTypeDetail-&gt;rent / $fp-&gt;unitTypeDetail-&gt;base_sqft);
                      $offset = $sqftDiff * $brpsf * ($this-&gt;property-&gt;rate/100);
                      //                                    $round_offset = round($offset,2);
                  }
  
                  if($fp-&gt;unitTypeDetail-&gt;rent  == null || $fp-&gt;sqft == null){
                      $rpsf = '';
                  }else{
                      if($offset == ''){
                          $rpsf = $fp-&gt;unitTypeDetail-&gt;rent / $fp-&gt;sqft;
                      }else{
                          $rpsf = ($fp-&gt;unitTypeDetail-&gt;rent + $offset) / $fp-&gt;sqft;
                      }
  
                  }
              }
  
              /** in_array test **/
              if(in_array('pms_property',$this-&gt;mapped_columns)){
                  $temp_items['PMS Property'] = $fp-&gt;pms_property;
              }
              $temp_items['PMS Unit Type'] = $fp-&gt;pms_unit_type;
  
              if(in_array('description',$this-&gt;mapped_columns)){
                  $temp_items['Description'] = $fp-&gt;description;
              }
              if(in_array('beds',$this-&gt;mapped_columns)){
                  $temp_items['Beds'] = $fp-&gt;beds;
              }
              if(in_array('baths',$this-&gt;mapped_columns)){
                  $temp_items['Baths'] = $fp-&gt;baths;
              }
              $temp_items['Sqft'] = $fp-&gt;sqft;
              $temp_items['Unit Count'] = $fp-&gt;unit_count;
  
              $temp_items['Unit Type'] = $fp-&gt;unitTypeDetail-&gt;unit_type;
              $temp_items['Base'] = ($fp-&gt;unitTypeDetail-&gt;base_floor_plan_id == $fp-&gt;id)?'x':'';
              $temp_items['Affordable'] = ($fp-&gt;affordable == 1)?'x':'';
              $temp_items['SqFtDiff'] = $sqftDiff;
  
              $pricingOffset = '';
              if(in_array('pricing_offset',$this-&gt;mapped_columns)){
                  $pricingOffset = $fp-&gt;pricing_offset;
                  $temp_items['Current Offset ($)'] = $pricingOffset;
              }
  
              $temp_items['RPSF ($)'] = empty($rpsf)?'-':round($rpsf, 2);
              $temp_items['Recommended Offset ($)'] = empty($offset)?'-':round($offset);
  
              if( (string)$temp_items['Current Offset ($)'] != &quot;&quot; &amp;&amp; (string)$offset != &quot;&quot;){
                  $diff = $pricingOffset - $offset;
                  if($diff != 0){
                      if($diff &gt; 0){
                          $overCountArr[] = $fp-&gt;unit_count;
                          $overChargeArr[] = $diff;
                      }else{
                          $underCountArr[] = $fp-&gt;unit_count;
                          $underChargeArr[] = abs($diff);
                      }
                  }
              }
  
              $items[] = $temp_items;
          }
  
  
  
          /** Data useful for second sheet*/
          $underAverage = $overAverage = $over50Average = 0;
          $totalUnderCount = array_sum($underCountArr);
          $totalUnderImpact = 0;
          $allOverCountArr = [];
          for($i=0; $i&lt;count($underCountArr); $i++){
              $totalUnderImpact += $underCountArr[$i] * $underChargeArr[$i];
          }
          if(!empty($totalUnderCount)){
              $underAverage = $totalUnderImpact/$totalUnderCount;
          }
  
          $totalOver50Count = $totalOver50Impact = 0;
          $totalOverCount = array_sum($overCountArr);
          $totalOverImpact = $over50CountArr = 0;
          for($i=0; $i&lt;count($overCountArr); $i++){
              $totalOverImpact += $overCountArr[$i] * $overChargeArr[$i];
              if($overChargeArr[$i] &gt; 50){
                  $totalOver50Count += $overCountArr[$i];
                  $totalOver50Impact += $overCountArr[$i] * $overChargeArr[$i];
              }
          }
          if(!empty($totalOverCount)){
              $overAverage = $totalOverImpact/$totalOverCount;
          }
          if(!empty($totalOver50Count)){
              $over50Average = $totalOver50Impact/$totalOver50Count;
          }
  
          /** Calculated data which  need to be shown in second sheet */
          $offset_summary = [
              ['UnderCharge'],
              ['Count', 'Average', 'Impact'],
              [$totalUnderCount, SanitizeData::currency(round(abs($underAverage))), SanitizeData::currency(round(abs($totalUnderImpact))) ], //just transform number to Currency
              [''],
              ['Affected Count', 'Per Unit Avg', 'Unit Count &gt; $50', 'Per Unit Avg &gt; $50'],
              [$totalOverCount, SanitizeData::currency(round($overAverage)), $totalOver50Count, SanitizeData::currency(round($over50Average))]
          ];
          return [
              'table'     =&gt; $items,
              'summary'    =&gt; $offset_summary
          ];
      }
  
  }
  
  </code></pre>
  <p><strong>secondExport</strong></p>
  <pre><code>&lt;?php
  
  namespace App\Exports;
  
  use App\Models\UnitTypeDetail;
  use Maatwebsite\Excel\Concerns\FromArray;
  
  class OffsetSummaryExport implements FromArray
  {
      protected $property;
  
      public function __construct($property)
      {
          $this-&gt;property = $property;
      }
  
      public function array(): array
      {
          $unitTypeDetails = UnitTypeDetail::where('property_id',$this-&gt;property-&gt;id)
              -&gt;get();
  
          $unitTypes[] = [
              'Unit Type',
              'Rent'
          ];
          foreach($unitTypeDetails as $uk =&gt; $uv){
              $unitTypes[] = [
                  $uv-&gt;unit_type,
                  $uv-&gt;rent
              ];
          }
          $unitType_data[] = $unitTypes;
          $unitType_data[] = [
              '',
              ''
          ];
          $unitType_data[] = [
              'Rate',
              $this-&gt;property-&gt;rate.'%'
          ];
  
          return $unitType_data;
      }
  }
  </code></pre>
  <p><strong>Main Requirements</strong></p>
  <p>Here, on class <code>OffetTableExport</code> you could see an array <code>offset_summary</code>, and I need this data to be shown on second sheet instead of first sheet. So, I tried to return this <code>offset_summary</code> with <code>$items</code> on first <code>OffetTableExport</code>.</p>
  <p>And tried to retrieve it and add it onto the array of second sheet as:</p>
  <pre><code>$resp_data = new OffetTableExport($this-&gt;property, $this-&gt;mapped_columns);
  $offset_summary = $offset + $resp_data['summary'];
  
  $sheets = [
      $resp_data['table'],
      $offset_summary
  ];
  </code></pre>
  <p>But showing error, exactly on <code>$offset_summary = $offset + $resp_data['summary'];</code> this line as:</p>
  <p><code>Cannot use object of type App\Exports\OffetTableExport as array</code></p>
  <p>Update:</p>
  <p>Strange but while dumping <code>resp_data</code> it shows:</p>
  <pre><code>App\Exports\OffetTableExport {#1789 ▼
    #property: App\Models\Property {#1657 ▼
      #table: &quot;properties&quot;
      #guarded: array:1 [▶]
      #dates: array:4 [▶]
      #connection: &quot;mysql&quot;
      #primaryKey: &quot;id&quot;
      #keyType: &quot;int&quot;
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:16 [▶]
      #original: array:16 [▶]
      #changes: []
      #casts: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
    }
    #mapped_columns: array:10 [▼
      0 =&gt; &quot;pms_unit_type&quot;
      1 =&gt; &quot;sqft&quot;
      2 =&gt; &quot;unit_count&quot;
      3 =&gt; &quot;unit_type&quot;
      4 =&gt; null
      5 =&gt; null
      6 =&gt; null
      7 =&gt; &quot;pricing_offset&quot;
      8 =&gt; null
      9 =&gt; null
    ]
  }
  </code></pre>