Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sandonet's avatar

Undefined variable: products

Hello, i need some help here, this undefined variable error is give me a lot of headache. The controller has already give variable "products" to views. Please check this out, this is my controller


public function export()
    {
        $format = Input::query('type');
        $filename = Carbon::now()->format('Ymd_') . "ProductsList";
        $file = Excel::create($filename, function ($excel) {

            $excel->sheet('Product Cost and Amount', function ($sheet) {
                $sheet->setfitToPage(0);
                $sheet->setfitToWidth(0);
                $sheet->setfitToHeight(0);
                $sheet->freezeFirstRowAndColumn();
                $sheet->setOrientation('landscape');
                $products = $this->report->getProductLevels();
                $sheet->loadView('reports.product_report.product_level_report')->with(compact('products'));

            });

            $excel->sheet('Product Locations', function ($sheet) {
                $sheet->setfitToPage(0);
                $sheet->setfitToWidth(0);
                $sheet->setfitToHeight(0);
                $sheet->freezeFirstRowAndColumn();
                $products = $this->report->getProductLevels();
                $sheet->loadView('reports.product_report.product_warehouse_level')->with(compact('products'));

            });


        });

this my product_level_report.blade.php

<html>
{{ HTML::style('dist/css/table.css') }}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<body>
<!-- Headings -->
<thead>
<tr>
    <th>#</th>
    <th>Product Name</th>
    <th>Amount</th>
    <th>Reorder Amount</th>
    <th>Unit Cost</th>
    <th>Selling Cost</th>
    <th>Tax rate</th>
    <th>Category</th>
</tr>
</thead>
<?php $i = 1; ?>
@foreach($products as $product)
    <tr>
        <td scope="row">{{$i}}</td>
        <td><b>{{$product->productName}}</b></td>
        <td>{{$product->amount}}</td>
        <td>{{$product->reorderAmount}}</td>
        <td>{{$product->unitCost}}</td>
        <td>{{$product->sellingCost}}</td>
        <td>{{$product->productTaxRate}}%</td>
        <td>{{$product->categoryName}}</td>
    </tr>
    <?php $i++; ?>
@endforeach
</body>
</html>

I don't know anymore what i need to fix, glad if anyone can help to solve this, thank you.

0 likes
2 replies
topvillas's avatar
$sheet->loadView('reports.product_report.product_level_report', compact('products'));
1 like

Please or to participate in this conversation.