Search code examples
excellaravelmaatwebsite-excel

Get() not working propely


i have a code where i need to read a csv and save on my database , but, fot read they need to convert to a collection ,the method "->get()" should do it , but is not,PhpStorm is saying **the method is not found in void ** ,so ... i dont have more ideias ,i apreciate if someone could help me , i put the cde bellow

public function importExcel(Request $request)
{
    dump("passo 1");
    if($request->hasFile('import_file')){
        dump("passo 2");
        $file=Excel::load($request->file('import_file')->getRealPath(),function($reader){ })->get();
        dd($file);
        dd("fim");
    };
}

Solution

  • You can get the data in the closer passing variable in the function and you can do either

    $reader->get();
    

    or

    $reader->all();
    

    or you can loop the data and save it in ur model

    Excel::load($request->file('import_file'), function ($reader) {
    
      foreach ($reader->toArray() as $row) {
         User::firstOrCreate($row);
      }
    });
    

    if you need the variable outside the scope add & before the variable.

    function(&$reader) {}