I am using laravel framework 5.2. I am using Maatwebsite Excel package i have succesfully installed and succesfully imported CSV format files but problem is:
Suppose i have one table and columns are:
Table_name:- employees_schedule
columns:- user_id, customer_name, date,
Now when i upload CSV file with three columns (user_id, customer_name, date) it is successfully upload.
When I upload CSV format file, with additional columns example (user_id, customer_name, date, hacking_column, time) I need to show an error message, something like "Your CSV files has some unwanted columns"
Can anyone help me. Here is my function
public function UploadSchedule (Request $request)
{
if ($request->isMethod('post')) {
$data = $request->all();
//echo "<pre>"; print_r($data); die;
Excel::load(Input::file('schedule'), function ($reader) {
$reader->each(function ($sheet) {
EmployeeSchedule::firstOrCreate($sheet->toArray());
});
});
return redirect()
->back()
->with(
'flash_message_success',
'Your Employee Schedule Uploaded successfully!'
);
}
}
And blade file:-
<form id="addForm" role="form" class="form-horizontal" method="post"
action="{{ url('admin/upload-employee-schedule') }}"
enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}"/>
<div class="form-body">
<div class="form-group">
<label class="col-md-3 control-label">Upload Schedule:</label>
<div class="col-md-5">
<input type="file" id="csv" name="schedule">
</div>
</div>
</div>
<div class="form-actions right1 text-center">
<button id="check" class="btn green" type="submit">Submit</button>
</div>
</form>