Cannot use object of type App\Exports\OffetTableExport as array - Laravel Excel

牧云@^-^@ 提交于 2021-01-05 08:57:41

问题


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:

mainExport

<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class FloorPlanExport implements WithMultipleSheets
{
    protected $property, $mapped_columns;

    public function __construct($property, $mapped_columns)
    {
        $this->property = $property;
        $this->mapped_columns = $mapped_columns;
    }

    /**
     * @return array
     */
    public function sheets(): array
    {
        $offset = new OffsetSummaryExport($this->property);
        $resp_data = new OffetTableExport($this->property, $this->mapped_columns);
        $offset_summary = $offset + $resp_data['summary'];

        $sheets = [
            $resp_data['table'],
            $offset_summary
        ];
        return $sheets;
    }


}

exportRelatedToFirstSheet

<?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->property = $property;
        $this->mapped_columns = $mapped_columns;
    }

    public function array(): array
    {
        $floorPlans = FloorPlan::with(['unitTypeDetail'])
            ->where('property_id',$this->property->id)
            ->get();

        $items = [];
        $underCountArr = $overCountArr = [];
        $underChargeArr = $overChargeArr = [];
        foreach($floorPlans as $fk => $fp){
            $temp_items = [];
            if($fk === 0 ){
                if(in_array('pms_property',$this->mapped_columns)){
                    $temp_items['PMS Property'] = 'PMS Property';
                }
                $temp_items['PMS Unit Type'] = 'PMS Unit Type';

                if(in_array('description',$this->mapped_columns)){
                    $temp_items['Description'] = 'Description';
                }
                if(in_array('beds',$this->mapped_columns)){
                    $temp_items['Beds'] = 'Beds';
                }
                if(in_array('baths',$this->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->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->affordable == 1){
                $sqftDiff = '';
                $rpsf = '';
                $offset = '';
            }else{
                if($fp->unitTypeDetail->base_sqft == null || $fp->sqft == null){
                    $sqftDiff = '';
                }else{
                    $sqftDiff = $fp->sqft - $fp->unitTypeDetail->base_sqft;
                }

                if($this->property->rate  === null || $sqftDiff === '' || $fp->unitTypeDetail->base_sqft === null){
                    $offset = '';

                }else{
                    $brpsf =  ($fp->unitTypeDetail->rent / $fp->unitTypeDetail->base_sqft);
                    $offset = $sqftDiff * $brpsf * ($this->property->rate/100);
                    //                                    $round_offset = round($offset,2);
                }

                if($fp->unitTypeDetail->rent  == null || $fp->sqft == null){
                    $rpsf = '';
                }else{
                    if($offset == ''){
                        $rpsf = $fp->unitTypeDetail->rent / $fp->sqft;
                    }else{
                        $rpsf = ($fp->unitTypeDetail->rent + $offset) / $fp->sqft;
                    }

                }
            }

            /** in_array test **/
            if(in_array('pms_property',$this->mapped_columns)){
                $temp_items['PMS Property'] = $fp->pms_property;
            }
            $temp_items['PMS Unit Type'] = $fp->pms_unit_type;

            if(in_array('description',$this->mapped_columns)){
                $temp_items['Description'] = $fp->description;
            }
            if(in_array('beds',$this->mapped_columns)){
                $temp_items['Beds'] = $fp->beds;
            }
            if(in_array('baths',$this->mapped_columns)){
                $temp_items['Baths'] = $fp->baths;
            }
            $temp_items['Sqft'] = $fp->sqft;
            $temp_items['Unit Count'] = $fp->unit_count;

            $temp_items['Unit Type'] = $fp->unitTypeDetail->unit_type;
            $temp_items['Base'] = ($fp->unitTypeDetail->base_floor_plan_id == $fp->id)?'x':'';
            $temp_items['Affordable'] = ($fp->affordable == 1)?'x':'';
            $temp_items['SqFtDiff'] = $sqftDiff;

            $pricingOffset = '';
            if(in_array('pricing_offset',$this->mapped_columns)){
                $pricingOffset = $fp->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 ($)'] != "" && (string)$offset != ""){
                $diff = $pricingOffset - $offset;
                if($diff != 0){
                    if($diff > 0){
                        $overCountArr[] = $fp->unit_count;
                        $overChargeArr[] = $diff;
                    }else{
                        $underCountArr[] = $fp->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<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<count($overCountArr); $i++){
            $totalOverImpact += $overCountArr[$i] * $overChargeArr[$i];
            if($overChargeArr[$i] > 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 > $50', 'Per Unit Avg > $50'],
            [$totalOverCount, SanitizeData::currency(round($overAverage)), $totalOver50Count, SanitizeData::currency(round($over50Average))]
        ];
        return [
            'table'     => $items,
            'summary'    => $offset_summary
        ];
    }

}

secondExport

<?php

namespace App\Exports;

use App\Models\UnitTypeDetail;
use Maatwebsite\Excel\Concerns\FromArray;

class OffsetSummaryExport implements FromArray
{
    protected $property;

    public function __construct($property)
    {
        $this->property = $property;
    }

    public function array(): array
    {
        $unitTypeDetails = UnitTypeDetail::where('property_id',$this->property->id)
            ->get();

        $unitTypes[] = [
            'Unit Type',
            'Rent'
        ];
        foreach($unitTypeDetails as $uk => $uv){
            $unitTypes[] = [
                $uv->unit_type,
                $uv->rent
            ];
        }
        $unitType_data[] = $unitTypes;
        $unitType_data[] = [
            '',
            ''
        ];
        $unitType_data[] = [
            'Rate',
            $this->property->rate.'%'
        ];

        return $unitType_data;
    }
}

Main Requirements

Here, on class OffetTableExport you could see an array offset_summary, and I need this data to be shown on second sheet instead of first sheet. So, I tried to return this offset_summary with $items on first OffetTableExport.

And tried to retrieve it and add it onto the array of second sheet as:

$resp_data = new OffetTableExport($this->property, $this->mapped_columns);
$offset_summary = $offset + $resp_data['summary'];

$sheets = [
    $resp_data['table'],
    $offset_summary
];

But showing error, exactly on $offset_summary = $offset + $resp_data['summary']; this line as:

Cannot use object of type App\Exports\OffetTableExport as array

Update:

Strange but while dumping resp_data it shows:

App\Exports\OffetTableExport {#1789 ▼
  #property: App\Models\Property {#1657 ▼
    #table: "properties"
    #guarded: array:1 [▶]
    #dates: array:4 [▶]
    #connection: "mysql"
    #primaryKey: "id"
    #keyType: "int"
    +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 => "pms_unit_type"
    1 => "sqft"
    2 => "unit_count"
    3 => "unit_type"
    4 => null
    5 => null
    6 => null
    7 => "pricing_offset"
    8 => null
    9 => null
  ]
}

来源:https://stackoverflow.com/questions/65466300/cannot-use-object-of-type-app-exports-offettableexport-as-array-laravel-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!