Laravel-excel: Serialization of closure failed: Serialization of 'Illuminate\Http\UploadedFile' is not allowed

Created on 13 Jun 2017  ·  3Comments  ·  Source: Maatwebsite/Laravel-Excel

[QUESTION]

maatwebsite/excel 2.1, Laravel 5.4

Hello, guys. I am trying to import an excel file, but i get an error starting that "Serialization of closure failed: Serialization of 'Illuminate\Http\UploadedFile' is not allowed". I've tried some troubleshooting but I do not understand why this problem still happens. This is the source code I use to handle import excel files.

if($request->hasFile('file')){
     $file = $request->file('file')->getRealPath();
     \Excel::filter('chunk')->load($file)->chunk(200, function($result) use ($request){
        foreach ($result as $item) {
           if(collect($item)->has('title') && collect($item)->has('category_id')  && collect($item)->has('file')){
                Content::create([
                     'title' => $item->title,
                     'category_id' => $item->category_id,
                     'file' => $item->file,
                     'reference' => $request->input('reference')
                ]);
           }
        }
       });
      return redirect()->back();
}

Please help me to solve this problem.

Most helpful comment

You can't pass $request into the closure, as that gets serialized and queued. You have to pass explicit variables.

Try this:

if($request->hasFile('file')){
     $file = $request->file('file')->getRealPath();
    $reference = $request->input('reference');
     \Excel::filter('chunk')->load($file)->chunk(200, function($result) use ($reference){
        foreach ($result as $item) {
           if(collect($item)->has('title') && collect($item)->has('category_id')  && collect($item)->has('file')){
                Content::create([
                     'title' => $item->title,
                     'category_id' => $item->category_id,
                     'file' => $item->file,
                     'reference' => $reference
                ]);
           }
        }
       });
      return redirect()->back();
}

All 3 comments

You can't pass $request into the closure, as that gets serialized and queued. You have to pass explicit variables.

Try this:

if($request->hasFile('file')){
     $file = $request->file('file')->getRealPath();
    $reference = $request->input('reference');
     \Excel::filter('chunk')->load($file)->chunk(200, function($result) use ($reference){
        foreach ($result as $item) {
           if(collect($item)->has('title') && collect($item)->has('category_id')  && collect($item)->has('file')){
                Content::create([
                     'title' => $item->title,
                     'category_id' => $item->category_id,
                     'file' => $item->file,
                     'reference' => $reference
                ]);
           }
        }
       });
      return redirect()->back();
}

Thanks @patrickbrouwers. That's saved my live :+1: .

Thanks @patrickbrouwers.

Was this page helpful?
0 / 5 - 0 ratings