This question already has an answer here:
error:=> Call to undefined method Illuminate\Database\Query\Builder::lists()
And how to avoid duplicate data in .CSV file type..
public function import(Request $request){
$this -> validate($request, [
'import_file' => 'required|mimes:csv,xlsx|max:2048',
]);
if($request->hasFile('import_file')){
$rollno = Student::lists('rollno')->toArray();
$file = $request->file('import_file') ;
$fileName = date('Y-m-d_H-i-s')."-".$file->getClientOriginalName();
$destinationPath = public_path().'/uploads/' ;
$file->move($destinationPath,$fileName);
$path=$destinationPath.$fileName;//"uploads/testfile_xlsx.xlsx";
$data = Excel::load($path, function($reader) {})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
if (in_array($value->rollno, $rollno))
continue;
$insert[] = ['name' => $value->name,'rollno' => $value->rollno,'department' => $value->department, 'course' => $value->course,'image_name'=>''];
}
if(!empty($insert)){
DB::table('students')->insert($insert);
return back()->with('success','Insert Record successfully.');
}
}
else{unlink($path); return back()->with('error','Please Check your file. No Rows'); }
}
return back()->with('error','Something is wrong there.');
}
</div>