Home > Software design >  How to fix this error in laravel 8.10.1 : Undefined constant "App\Exports\id"
How to fix this error in laravel 8.10.1 : Undefined constant "App\Exports\id"

Time:01-02

I'm getting this error while calling the export function of laravel Excel. Can anyone help me solve this error or why am I getting it???

Laravel Version: 8.10.1

Laravel Excel Package Version: 3.1

Error Screenshot

My Code in Controller:

namespace App\Http\Controllers;

use App\Exports\ProductInventoryExport;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Maatwebsite\Excel\Facades\Excel;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function ProductInventory()
    {
        return Excel::download(new \App\Exports\ProductInventoryExport, 'ProductInventory.xlsx');

    }
}

My Code in Export Class File:

namespace App\Exports;

use App\Models\Product;
use App\Models\AttributeValue;
use App\Models\Category;
use App\Models\Subcategory;
use App\Models\User;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Excel;
use Illuminate\Support\Facades\Schema;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\WithHeadings;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use \Maatwebsite\Excel\Writer;
use \Maatwebsite\Excel\Sheet;
use Carbon\Carbon;

Writer::macro('setCreator', function (Writer $writer, string $creator) {
    $writer->getDelegate()->getProperties()->setCreator($creator);
});


Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
    $sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
});

Sheet::macro('setOrientation', function (Sheet $sheet, $orientation) {
    $sheet->getDelegate()->getPageSetup()->setOrientation($orientation);
});


class ProductInventoryExport implements FromArray, ShouldAutoSize, WithHeadings, WithStyles
{
    /**
    * @return \Illuminate\Support\collection
    */

    // STYLES

    public $columns = [id, image, name, slug, product_type, size, colour, tax_percent, cost_price, regular_price, sale_price, SKU, quantity, stock_status, sold, purchase_limit, featured, short_description, description, category_id, subcategory_id, created_at, updated_at];

    public $HeadingStyle = [
        'borders' => [
            'allBorders' => [
                'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
            ],
        ],
        'alignment' => [
            'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT,
            'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
        ],
        'font' => [
            'bold' => true,
            'size' => 12,
        ],
    ];

    public $ColumnStyle = [
        'borders' => [
            'allBorders' => [
                'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
            ],
        ],
        'alignment' => [
            'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
        ],
        'font' => [
            'bold' => true,
            'size' => 8,
        ],
    ];

    public $CellStyle = [
        'borders' => [
            'allBorders' => [
                'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
            ],
        ],
        'alignment' => [
            'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
        ],
        'font' => [
            'bold' => false,
            'size' => 8,
        ],
    ];

    // FUNCTIONS

    public function headings(): array
    {
        // $columns = Schema::getColumnListing('products');
        // $ar = [];
        // foreach($columns as $column)
        // {
        //     array_push($ar, ucwords(trans(preg_replace('/__*/', ' ', $column))));
        // }
        return [
            ['Rojanic Bangladesh'],
            ['In Stock Products - '.Carbon::now()->month.' '.Carbon::now()->year],
            ['ID', 'Image', 'Name', 'Slug', 'Type', 'Size', 'Colour', 'Vat Percent', 'Cost Price', 'Regular Price', 'Sale Price', 'Barcode', 'Left Quantity', 'Stock', 'Sold', 'Purchase Limit', 'Featured', 'Short Description', 'Description', 'Category', 'Sub Category', 'Created At', 'Last Updated'],
            // $ar
        ];
    }

    public function styles(Worksheet $sheet)
    {

        $columnTill = 'a';
        foreach(Schema::getColumnListing('products') as $colum)
        {
            $columnTill  ;
        }
        --$columnTill;
        $productCount = Product::all()->count();
        $sheet->mergeCells('A1:'.$columnTill.'1', Worksheet::MERGE_CELL_CONTENT_MERGE);
        $sheet->mergeCells('A2:'.$columnTill.'2', Worksheet::MERGE_CELL_CONTENT_MERGE);
        $sheet->getStyle('A1:'.$columnTill.'1')->applyFromArray($this->HeadingStyle);
        $sheet->getStyle('A2:'.$columnTill.'2')->applyFromArray($this->HeadingStyle);
        $sheet->getStyle('A3:'.$columnTill.'3')->applyFromArray($this->ColumnStyle);
        $sheet->getStyle('A4:'.$columnTill.Product::all()->count() 3)->applyFromArray($this->CellStyle);
    }

    public function array(): array
    {
        $ready = [];
        foreach(Product::all() as $product)
        {
            $part = [];
            $attributes = AttributeValue::where('product_id',$product->id)->get();
            $productCategory = null;
            $productSubCategory = null;
            if($product->category_id)
            {
                $productCategory = Category::find($product->category_id);
            }
            if($product->subcategory_id)
            {
                $productCategory = Subcategory::find($product->subcategory_id);
            }
            if($attributes->count() > 0)
            {
                foreach($attributes as $attribute)
                {
                    array_push($part,
                        $product->id,
                        $attribute->image ? $attribute->image : $product->image,
                        $product->name,
                        $product->slug,
                        $product->product_type,
                        $attribute->size,
                        $attribute->colour,
                        $product->tax_percent,
                        $attribute->cost_price ? $attribute->cost_price : $product->cost_price,
                        $attribute->regular_price ? $attribute->regular_price : $product->regular_price,
                        $attribute->sale_price ? $attribute->sale_price : $product->sale_price,
                        $product->SKU,
                        $attribute->quantity,
                        $attribute->quantity > 0 ? 'In Stock' : 'Out Of Stock',
                        OrderItem::where('product_id',$product->id)->where('attribute_id',$attribute->id)->count(),
                        $product->purchase_limit,
                        $product->featured,
                        $product->short_description,
                        $product->description,
                        $productCategory,
                        $productSubCategory,
                        $attribute->created_at,
                        $attribute->updated_at
                    );
                }
            }
            else
            {
                array_push($part,
                    $product->id,
                    $product->image,
                    $product->name,
                    $product->slug,
                    $product->product_type,
                    'N/A',
                    'N/A',
                    $product->tax_percent,
                    $product->cost_price,
                    $product->regular_price,
                    $product->sale_price,
                    $product->SKU,
                    $product->quantity,
                    $product->quantity > 0 ? 'In Stock' : 'Out Of Stock',
                    OrderItem::where('product_id',$product->id)->count(),
                    $product->purchase_limit,
                    $product->featured,
                    $product->short_description,
                    $product->description,
                    $productCategory,
                    $productSubCategory,
                    $product->created_at,
                    $product->updated_at
                );
            }
            array_push($ready, $part);
        }
        return
            $ready
        ;
    }
}

CodePudding user response:

In this line:

public $columns = [id, image, name, slug, product_type, size, colour, tax_percent, cost_price, regular_price, sale_price, SKU, quantity, stock_status, sold, purchase_limit, featured, short_description, description, category_id, subcategory_id, created_at, updated_at];

you should enclose each array elements using single or double quotes. If you don't, PHP treats them as constants, and since there's no 'id' constant in the App\Exports namespace, PHP throws that error.

  • Related