php - Upload CSV to SQL using laravel eloquent -
i have problem in uploading csv database (sql). using maatwebsite... , here's controller:
class uploadcsv extends controller { public function store(request $request){ if($request->hasfile('import_file')){ $path = $request->file('import_file')->getrealpath(); $data = \excel::load($path)->get(); if($data->count()){ foreach ($data $key => $value) { $arr[] = ['s_id' => $value->id, 'school_name' => $value->sname, 'region' => $value->reg, 'province' => $value->prov, 'municipality' => $value->mun, 'division' => $value->div, 'district' => $value->dis, 'enrollment_sy_2014_2015' => $value->enrolled, 'mooe_in_php_for_fy_2015' => $value->mooe, 'latitude' => $value->lat, 'longitude' => $value->lng ]; } map::insert($arr); dd('insert record successfully.'); //return json_encode($arr); } } dd('request data not have files import.'); }
which gives me endless error message:
the csv contains 200+ rows. appreciated. in advance :))
maybe try this, create new model (assuming map
name of model , save()
:
<?php class uploadcsv extends controller { public function store(request $request){ if($request->hasfile('import_file')){ $path = $request->file('import_file')->getrealpath(); $data = \excel::load($path)->get(); if($data->count()){ foreach ($data $key => $value) { $entry = new map; $entry->s_id = $value->id; $entry->school_name = $value->sname; $entry->region = $value->reg; $entry->province = $value->prov; $entry->municipality = $value->mun; $entry->division = $value->div; $entry->district = $value->dis; $entry->enrollment_sy_2014_2015 = $value->enrolled; $entry->mooe_in_php_for_fy_2015 = $value->mooe; $entry->latitude = $value->lat; $entry->longitude = $value->lng; $entry->save(); } } } dd('request data not have files import.'); } }
Comments
Post a Comment