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

stargatesg1's avatar

Post form data into Excel file

I am calling the following function in my controllers processForm and I am using

https://github.com/Maatwebsite/Laravel-Excel

To export into a excel file. Problem is that the $request object loses scope when calling the export to excel. Is there anyway to solve this.

 public function processForm(Request $request)
    {
        $formData="";

        $formData= $request->all();

        Excel::create('data', function($excel) {


            $excel->sheet('Sheetname', function($sheet) {

                $sheet->fromArray($frmData);

            });

        })->store('xls', public_path('uploads'));
}
0 likes
5 replies
bobbybouwmann's avatar

You can simply do this

 public function processForm(Request $request)
{
    $formData= $request->all();

    // Notice the use statement here, you need to provided that for a callback function
    Excel::create('data', function($excel) use ($formData) {
        // Notice the use statement here
        $excel->sheet('Sheetname', function($sheet) use ($formData) {
            $sheet->fromArray($frmData);
        });
    })->store('xls', public_path('uploads'));
}

Source: http://stackoverflow.com/questions/4588714/php-callback-function-using-variables-calculated-outside-of-it

jacob_v_dam's avatar

You should change $frmData into $formData

public function processForm(Request $request)
{
    $formData= $request->all();

    // Notice the use statement here, you need to provided that for a callback function
    Excel::create('data', function($excel) use ($formData) {
        // Notice the use statement here
        $excel->sheet('Sheetname', function($sheet) use ($formData) {
            $sheet->fromArray($formData);
        });
    })->store('xls', public_path('uploads'));
}
stargatesg1's avatar

I agree with you jacob I also tried to append to the file and it has the correct permissions on the server but it will not work for some strange reason. Not sure what I am missing.

public function processForm(Request $request)
{
    $formData= $request->all();

    // Notice the use statement here, you need to provided that for a callback function
      Excel::load(public_path('uploads').'/data.xls', function($reader) use ($formData) {
            // Notice the use statement here
            $reader->sheet('Sheetname', function($sheet) use ($formData) {
                $sheet->appendRow($formData);
            });
        })->export('xls');
}

stargatesg1's avatar

The following works when calling the url directly. When using a form post it works only when creating a new file. When prependRow is called the application doesn't work. Any ideas as to what I am doing wrong.

/*Routes */
Route::get('xlsrow','ProcessFormController@addrow');

In my controller

  public function addrow(){
        Excel::load(public_path('uploads').'/data.xls', function($reader)
        {

            $reader->sheet('Sheetname',function($sheet)  {

                $sheet->prependRow(array(
                    'prepended', 'prepended'
                ));
            });

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

Here is process form

   public function processForm(Request $request)
    {


        $formData= $request->all();



        Excel::load(public_path('uploads').'/data.xls', function($reader) use ($formData)
        {

            $reader->sheet('Sheetname',function($sheet) use ($formData) {

                $sheet->prependRow($formData);
            });

        })->export('xls');
}
stargatesg1's avatar

So I figured it out the solution was adding the following

->store('xls', public_path('/uploads/'), false);

2 likes

Please or to participate in this conversation.