Add ability to choose which columns of your CSV will be used for importing
MODIFY
app/Http/Controllers/ImportController.php
app/Http/Controllers/ImportController.php
@@ -34,4 +34,37 @@ class ImportController extends Controller {
return redirect()->route('imports.index');
}
+
+ public function getPrepare(Import $import) {
+ // TODO AUTHORIZE
+
+ $headers = [];
+
+ $file = fopen(storage_path('app/imports/' . $import->file), 'r');
+
+ while ($row = fgetcsv($file, 999, ',')) {
+ foreach ($row as $column) {
+ $headers[] = $column;
+ }
+
+ break;
+ }
+
+ return view('imports.prepare', compact('headers'));
+ }
+
+ public function postPrepare(Request $request, Import $import) {
+ // TODO AUTHORIZE
+
+ // TODO VALIDATE
+
+ $import->fill([
+ 'column_happened_on' => $request->input('column_happened_on'),
+ 'column_description' => $request->input('column_description'),
+ 'column_amount' => $request->input('column_amount'),
+ 'status' => 1
+ ])->save();
+
+ return redirect()->route('imports.index');
+ }
}

MODIFY
app/Import.php
app/Import.php
@@ -8,5 +8,13 @@ use Illuminate\Database\Eloquent\SoftDeletes;
class Import extends Model {
use SoftDeletes;
- protected $fillable = ['space_id', 'name', 'file'];
+ protected $fillable = [
+ 'space_id',
+ 'name',
+ 'file',
+ 'status',
+ 'column_happened_on',
+ 'column_description',
+ 'column_amount'
+ ];
}

MODIFY
resources/views/imports/index.blade.php
resources/views/imports/index.blade.php
@foreach ($imports as $import)
<div class="box__section row">
<div class="row__column">{{ $import->name }}</div>
- <div class="row__column">{{ $import->status }}</div>
+ <div class="row__column">{{ $import->status }} {!! $import->status == 0 ? '<a href="/imports/' . $import->id . '/prepare">Prepare</a>' : '' !!}</div>
</div>
@endforeach
@else

ADD
resources/views/imports/prepare.blade.php
+<form method="POST">
+ {{ csrf_field() }}
+ <div class="row">
+ <div class="row__column">
+ <label>{{ __('fields.date') }}</label>
+ <select name="column_happened_on">
+ @foreach ($headers as $index => $header)
+ <option value="{{ $index }}">{{ $header }}</option>
+ @endforeach
+ </select>
+ </div>
+ <div class="row__column">
+ <label>{{ __('fields.description') }}</label>
+ <select name="column_description">
+ @foreach ($headers as $index => $header)
+ <option value="{{ $index }}">{{ $header }}</option>
+ @endforeach
+ </select>
+ </div>
+ <div class="row__column">
+ <label>{{ __('fields.amount') }}</label>
+ <select name="column_amount">
+ @foreach ($headers as $index => $header)
+ <option value="{{ $index }}">{{ $header }}</option>
+ @endforeach
+ </select>
+ </div>
+ </div>
+ <button>Submit</button>
+</form>
\ No newline at end of file

MODIFY
routes/web.php
routes/web.php
@@ -49,6 +49,8 @@ Route::group(['middleware' => ['auth']], function () {
Route::get('/imports', 'ImportController@index')->name('index');
Route::get('/imports/create', 'ImportController@create')->name('create');
Route::post('/imports', 'ImportController@store')->name('store');
+ Route::get('/imports/{import}/prepare', 'ImportController@getPrepare')->name('prepare');
+ Route::post('/imports/{import}/prepare', 'ImportController@postPrepare');
});
Route::get('/settings', 'SettingsController@index')->name('settings');

Add ability to choose which columns of your CSV will be used for importing