使用文本输入字段,我需要向通过CSV导入模块导入的记录添加标签。在控制器中,我会自动将用户ID,团队ID和时间戳添加到所有导入的记录中,现在我需要在导入时将用户定义的标签添加到所有记录中。
控制器中要实现此功能需要做什么?
<?php
`namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use SpreadsheetReader;
use Illuminate\Support\Facades\File;
class CsvImportController extends Controller
{
public function parse(Request $request)
{
$file = $request->file('csv_file');
$request->validate([
'csv_file' => 'mimes:csv,txt',
]);
$path = $file->path();
$hasHeader = $request->input('header', false) ? true : false;
$reader = new SpreadsheetReader($path);
$headers = $reader->current();
$lines = [];
$lines[] = $reader->next();
$lines[] = $reader->next();
$filename = str_random(10) . '.csv';
$file->storeAs('csv_import', $filename);
$modelName = $request->input('model', false);
$fullModelName = "App\\" . $modelName;
$model = new $fullModelName();
$fillables = $model->getFillable();
$redirect = url()->previous();
return view('csvImport.parse_import',
compact('headers', 'filename', 'fillables', 'hasHeader', 'modelName', 'lines', 'redirect'));
}
public function process(Request $request)
{
$filename = $request->input('filename', false);
$path = storage_path('app/csv_import/' . $filename);
$hasHeader = $request->input('hasHeader', false);
$fields = $request->input('fields', false);
$fields = array_flip(array_filter($fields));
$modelName = $request->input('modelName', false);
$model = "App\\" . $modelName;
$reader = new SpreadsheetReader($path);
$insert = [];
foreach ($reader as $key => $row) {
if ($hasHeader && $key == 0) {
continue;
}
$tmp = [];
foreach ($fields as $header => $k) {
$tmp[$header] = $row[$k];
}
if (auth()->user()->team_id) {
$tmp['created_by_id'] = auth()->user()->id;
$tmp['created_by_team_id'] = auth()->user()->team_id;
$tmp['created_at'] = now();
}
$insert[] = $tmp;
}
$for_insert = array_chunk($insert, 100);
foreach ($for_insert as $insert_item) {
$model::insert($insert_item);
}
$rows = count($insert);
$table = str_plural($modelName);
File::delete($path);
$redirect = $request->input('redirect', false);
return redirect()->to($redirect)->with('message', trans('global.app_imported_rows_to_table',
['rows' => $rows, 'table' => $table]));
}
我的视图:
<div class='row'>
<div class='col-md-12'>
<div class="panel panel-default">
<div class="panel-heading">
@lang('global.app_csvImport')
</div>
<div class="panel-body table-responsive">
<form class="form-horizontal" method="POST" action="{{ route('admin.csv_process') }}">
{{ csrf_field() }}
<input type="hidden" name="filename" value="{{ $filename }}"/>
<input type="hidden" name="hasHeader" value="{{ $hasHeader }}"/>
<input type="hidden" name="modelName" value="{{ $modelName }}"/>
<input type="hidden" name="redirect" value="{{ $redirect }}"/>
<table class="table">
@if (isset($headers))
<tr>
@foreach ($headers as $field)
<th>{{ $field }}</th>
@endforeach
</tr>
@endif
@if($lines)
@foreach ($lines as $line)
<tr>
@foreach ($line as $field)
<td>{{ $field }}</td>
@endforeach
</tr>
@endforeach
@endif
<tr>
@foreach ($headers as $key => $header)
<td>
<select name="fields[{{ $key }}]">
<option value=''>Please select</option>
@foreach ($fillables as $k => $fillable)
<option value="{{ $fillable }}"
@if (strtolower($header) === strtolower($fillable)) selected @endif>{{ $fillable }}</option>
@endforeach
</select>
</td>
@endforeach
</tr>
</table>
<button type="submit" class="btn btn-primary" id="btn-import">
@lang('global.app_import_data')
</button>
<button type="submit" class="btn btn-primary" id="btn-import2">
@lang('global.app_import_data')
</button>
</form>
</div>
</div>
</div>