excellaravelmaatwebsite-excellaravel-upgrade

I need to export excel from laravel in the new version for Maatwebsite\Excel 3.1 . Laravel | Excel


Hi i already see the documentation for Maatwebsite\Excel and try to understand the new way to export files .. now i have this project which made with Laravel 5.1 and I upgrade it to 8 and fixed most of problems but im stuck with this Maatwebsite\Excel ( package )

how to rewrite this code in the new version ..

          ini_set("memory_limit", "-1");
        $requested = (array) $request->columns_to_export;
        return Excel::create('instances-' . date('Y-m-d'), function ($excel) use ($request, $requested) {
            $excel->sheet('instances', function ($sheet) use ($request, $requested) {
                $columns = [];
                foreach ($requested as $key => $column) {
                    if ($column == 'sku') {
                        $columns[] = 'SKU';
                    }
                    if ($column == 'material_name') {
                        $columns[] = 'Material Nmae';
                    }
                    if ($column == 'new_or_used') {
                        $columns[] = 'NEW OR USED';
                    }
                    if ($column == 'barcode') {
                        $columns[] = 'BARCODE';
                    }
                    if ($column == 'asset_or_sale') {
                        $columns[] = 'ASSEST OR SALE';
                    }
                    if ($column == 'cost') {
                        $columns[] = 'COST';
                    }
                }
                if (!empty($columns)) {
                    $sheet->appendRow($columns);
                }
                $this->data['query']->chunk(500, function ($_500) use ($sheet, $request, $requested) {
                    foreach ($_500 as $o => $INSTANCE) {
                        $row     = [];
                        foreach ($requested as $key => $column) {
                            if ($column == 'sku') {
                                $row[]     = (string) '[' . $INSTANCE->sku . ']';
                            }
                            if ($column == 'material_name') {
                                $row[]     = (string) (empty($INSTANCE->Material) ? '------' : $INSTANCE->Material->{'name_' . app()->getLocale()});
                            }
                            if ($column == 'new_or_used') {
                                $row[]     = (string) ($INSTANCE->is_new == 'Y' ? 'NEW' : 'secondhand');
                            }
                            if ($column == 'barcode') {
                                $row[]     = (string) '[' . $INSTANCE->barcode . ']';
                            }
                            if ($column == 'asset_or_sale') {
                                $row[]     = (string) ($INSTANCE->is_asset == 'Y' ? 'ASSET' : ' not asset');
                            }
                            if ($column == 'cost') {
                                $row[]     = (string) round($INSTANCE->cost_of_good_sold, 2);
                            }
                            if ($column == 'last_stock') {
                                $row[]     = (string) (empty($INSTANCE->LastOrderEntry) ? '------' : ($INSTANCE->LastOrderEntry->Stock ? $INSTANCE->LastOrderEntry->Stock->{'name_' . app()->getLocale()} : '------'));
                            }
                            
                           else {
                                        $row[] = '------';
                                    }
                                }
                            }
                        }
                        if (!empty($row)) {
                            $sheet->appendRow($row);
                        }
                    }
                });
            });
        })
            ->download('xlsx');

Solution

  • ok For people who want to know Know How to rewrite the same code in New Version .. i just used heading : for this

    foreach ($requested as $key => $column) {
                    if ($column == 'sku') {
                        $columns[] = 'SKU';
                    }
                    if ($column == 'material_name') {
                        $columns[] = 'Material Nmae';
                    }
                    if ($column == 'new_or_used') {
                        $columns[] = 'NEW OR USED';
                    }
                    if ($column == 'barcode') {
                        $columns[] = 'BARCODE';
                    }
                    if ($column == 'asset_or_sale') {
                        $columns[] = 'ASSEST OR SALE';
                    }
                    if ($column == 'cost') {
                        $columns[] = 'COST';
                    }
                }
    

    and Mapping For this

      $this->data['query']->chunk(500, function ($_500) use ($sheet, $request, $requested) {
                    foreach ($_500 as $o => $INSTANCE) {
                        $row     = [];
                        foreach ($requested as $key => $column) {
                            if ($column == 'sku') {
                                $row[]     = (string) '[' . $INSTANCE->sku . ']';
                            }
                            if ($column == 'material_name') {
                                $row[]     = (string) (empty($INSTANCE->Material) ? '------' : $INSTANCE->Material->{'name_' . app()->getLocale()});
                            }
                            if ($column == 'new_or_used') {
                                $row[]     = (string) ($INSTANCE->is_new == 'Y' ? 'NEW' : 'secondhand');
                            }
                            if ($column == 'barcode') {
                                $row[]     = (string) '[' . $INSTANCE->barcode . ']';
                            }
                            if ($column == 'asset_or_sale') {
                                $row[]     = (string) ($INSTANCE->is_asset == 'Y' ? 'ASSET' : ' not asset');
                            }
                            if ($column == 'cost') {
                                $row[]     = (string) round($INSTANCE->cost_of_good_sold, 2);
                            }
                            if ($column == 'last_stock') {
                                $row[]     = (string) (empty($INSTANCE->LastOrderEntry) ? '------' : ($INSTANCE->LastOrderEntry->Stock ? $INSTANCE->LastOrderEntry->Stock->{'name_' . app()->getLocale()} : '------'));
                            }
                            
                           else {
                                        $row[] = '------';
                                    }
                                }
                            }
                        }
                        if (!empty($row)) {
                            $sheet->appendRow($row);
                        }
                    }
                });
            });
    

    so the new code will be somthing like this ..

    class MaterialInventoreyExport implements
        FromCollection,
        WithMapping,
        WithHeadings,
        ShouldAutoSize
    {
        use Exportable;
    
        protected $data;
        protected $requst;
    
        public function __construct($data, $requst)
        {
            $this->data = $data;
            $this->requst = $requst;
            // dd($this->data->get()[0]);
        }
    
        public function headings(): array
        {
            $requested = $this->requst;
            $columns = [];
            foreach ($requested as $key => $column) {
                        if ($column == 'sku') {
                            $columns[] = 'SKU';
                        }
                        if ($column == 'material_name') {
                            $columns[] = 'Material Nmae';
                        }
                        if ($column == 'new_or_used') {
                            $columns[] = 'NEW OR USED';
                        }
                        if ($column == 'barcode') {
                            $columns[] = 'BARCODE';
                        }
                        if ($column == 'asset_or_sale') {
                            $columns[] = 'ASSEST OR SALE';
                        }
                        if ($column == 'cost') {
                            $columns[] = 'COST';
                        }
                    }
            return $columns;
        }
    
    
        public function collection()
        {
            return $this->data->get();
        }
    
        public function map($data): array
        {
            $row = null;
            $requested = $this->requst;
            $A = [];
    
    
            foreach ($requested as $key => $column) {
                if ($column == 'sku') {
                    $A[] = $data->sku;
                }
                if ($column == 'material_name') {
                    $A[] = (string) (empty($data->Material) ? '------' : $data->Material->{'name_' . app()->getLocale()});
                }
                if ($column == 'new_or_used') {
                    $A[] =  (string) ($data->is_new == 'Y' ? 'new' : 'secondHand');
                }
                if ($column == 'barcode') {
                    $A[] =  (string) '[' . $data->barcode . ']';
                }
                if ($column == 'asset_or_sale') {
                    $A[] =  (string) ($data->is_asset == 'Y' ? 'asset' : 'Not Asset');
                }
                if ($column == 'cost') {
                    $A[] = (string) round($data->cost_of_good_sold, 2);
                }
                if ($column == 'last_stock') {
                    $A[] = (string) (empty($data->LastOrderEntry) ? '------' : 
                    ($data->LastOrderEntry->Stock ? $data->LastOrderEntry->Stock->{'name_' . app()->getLocale()} : '------'));
                }
            }
    
            return $A;
        }
    }