I'm having a problem while uploading excel file and update data to my database using Laravel 5.6 and Laravel Excel(maatwebsite 3.1) i can't get the contents of each column to update the data in my database.
$rows return empty
Model:namespace App;
use App\User;
use App\MyModel;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;class DataImport implements ToCollection
{
/**
* @param array $rows
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function collection(Collection $rows)
{
dd($rows);
foreach ($rows as $row) {
$import = MyModel::where(‘user_id’,$row[1])->first();
$import->field1= $row[2];
$import->field2= $row[5];
$import->field3= $row[3];
$import->status = ‘PAID’;
$import->save();
}}
}
Controller:
use Maatwebsite\Excel\Excel; public function postImport(Request $request){ if($request->hasFile('sample_file')){ Excel::import(new DataImport ,request()->file('sample_file')); return redirect('/import'); } }
Excel File:
--------------------------------------------------------------------------------
| # | USER ID | FIELD ONE | FIELD TWO | FIELD THREE | FIELD FOUR | FIELD FIVE || 1 | 1 | 123-abc | abc-123 | 123123 | asdf | konjesh |
#php #excel #laravel