Variable scope inside php function

Agu V

I'm doing an excel file export from an eloquent model using Laravel and Maatwebsite/Laravel-Excel.

Here's what I have

 public function export(Request $request)
    {
        $data = $this->getData($param1)->get();
        $export = Excel::create('example' . date('m-d-Y-G-His'), function ($excel) {
            $excel->setDescription('example');
        });

        $export->sheet('Example', function ($sheet) {
            $sheet->row(1, array(
                'row1',
                'row2',
                'row3'
            ));

            $i = 2;
            foreach ($data as $d) {
                $sheet->row($i, array(
                    $d->id,
                    $d->name, $d->date,
                ));
            }
        });
    }

And I get Undefined variable: data

I understand that is a scope problem and the error is correct, but what is the best way to pass the variable inside the function?

tkausl

You can ask PHP to make it available in the anonymous function with the use keyword:

$export->sheet('Example', function ($sheet) use ($data) {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Scope of a variable inside a function in R

PHP Variable scope within function

variable scope outside function in php

JS - variable scope and initilizing inside function

define outer scope variable inside function

Setting a global scope variable inside a function

vbscript variable scope inside class default function

Bash Script Variable Scope inside Function

static variable scope inside and outside the function

Referencing a variable inside a function inside a $scope member function in angularjs

PHP Variable Scope Inside Included Files

PHP - returning a variable variable from a function scope

Python supports global scope of variable but Php doesn't supports global scope variables inside a function without using the global keyword?

php static variable inside function

php variable inside onclick function?

AngularJS: variable $scope inside a function appears undefined outside the function

How to change a variable inside a function scope after defining the function in Python?

Accessing php variable from within function scope

Isolate scope variable is getting undefined inside link function

$scope variable being undefined despite being set inside a $watch function

Does a static variable inside a function ever go out of scope?

eventListener works on variable which is supposed to be inside a function's private scope

Get variable outside scope from a function inside an object - Javascript

Changing the global scope variable inside of a function with click handlers

Javascript: While the variable is declared in global scope, it remains undefined inside the function

Access value of the scope variable inside callback function in Angular2

Does python redefine guess of variable scope inside function?

Declaring an image variable in global scope is not getting assigned but inside a function does

Changing global integer variable from inside function scope