Laravel excel view

I am trying to export data to excel and I cannot get it to work. I am using: http://www.maatwebsite.nl/laravel-excel/docs

and I follow the documents as much as I can. So I have this point of view that I am trying to achieve. the docs tell me:

Loading a view for a single sheet

$sheet->loadView('folder.view');

      

So I did this and (of course) I get an error that the variables in the views are not being rejected since I am not passing variables to the view. So I read the docs again and it says:

Passing Variables to a View

Alternatively, you can use the with () method, which works the same as with Laravel views.

with a basic example with()

.

So now I have this in my controller function:

public function something(){
$something2='';
$something='';
Excel::create('Laravel Excel', function($excel) {

    $excel->sheet('Excel sheet', function($sheet) {
        $sheet->loadView('something')->with('something',$something)
                                     ->with('something2',$something2);
        $sheet->setOrientation('landscape');
    });

})->export('xls');
}

      

now i am getting error in controller:

undefined variable $ something

So I didn't know what could go wrong. I stayed positive and thought it would be something lightweight like $ something not actually being declared, so I removed that from the excel export. Then he gave me an error:

undefined variable $ something2

After that, I knew that something was wrong. Since both are undefined, according to excel creation. So I tried something new. Declaring both variables inside excel creation. Al though there is more code between declaring variables and creating excel. So it won't work. So the code looks like this:

$something2='';
$something='';
//more code
if($check==true){
Excel::create('Laravel Excel', function($excel) {

      

So, I am still stuck.

What's going on here? I don't understand what kind of creativity it is?

+3


source to share


2 answers


I think this might be a problem, since you are using cloasures, variables are not available in cloasure without defining their use.



public function something(){

    $something2='';
    $something='';

    Excel::create('Laravel Excel', function($excel) use ($something, $something2) {

        $excel->sheet('Excel sheet', function($sheet) use ($something, $something2) {
            $sheet->loadView('something')->with('something',$something)
                                         ->with('something2',$something2);
            $sheet->setOrientation('landscape');
        });

    })->export('xls');

}

      

+2


source


   ### Laravel excel passing a view ###

    For passing a varriable in excel you use laravel eager loading like under code :

    $productList = Products::selectProductList();

            Excel::create('Current Stock Report', function($excel) use ($productList) {

                $excel->sheet('Current Stock Report', function($sheet) use ($productList) {
                    $sheet->loadView('backend.report_pdf.current_stock_report_download')->with('productList',$productList);
                    $sheet->setOrientation('landscape');
                });

            })->export('xls');

      



0


source







All Articles