I want to import a csv file into database where name should always unique means if any duplicate title found it should avoid the row and go to the next row. How do I implement using Laravel Controller?
Here's the controller for importing csv/xl I've used :
public function importExcel()
{
if(Input::hasFile('import_file')){
$path = Input::file('import_file')->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
$insert[] = ['title' => $value->title, 'description' => $value->description];
}
if(!empty($insert)){
DB::table('items')->insert($insert);
// dd('Insert Record successfully.');
}
}
}
return back();
}