Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization of closure failed: Serialization of 'Illuminate\Http\UploadedFile' is not allowed #1238

Closed
kertul opened this issue Jun 13, 2017 · 3 comments

Comments

@kertul
Copy link

kertul commented Jun 13, 2017

[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.

@patrickbrouwers
Copy link
Member

patrickbrouwers commented Jun 13, 2017

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();
}

@ghost
Copy link

ghost commented Feb 21, 2018

Thanks @patrickbrouwers. That's saved my live 👍 .

@omd166
Copy link

omd166 commented Sep 3, 2018

Thanks @patrickbrouwers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants