public function import(Request $request) { /** * form is submitted check values and save if needed */ if ($request->isMethod('post')) { /** * validate data */ $rules = ['csv_file' => 'required']; $validator = Validator::make($request->all(), $rules); if ($validator->fails()) { $this->throwValidationException($request, $validator); } /** * save data if validated */ if ($request->file('csv_file')->isValid()) { //rename and move uploaded file $csv_file = Input::file('csv_file'); $tmp_import_name = time() . '_car_brand_import_.' . $csv_file->getClientOriginalExtension(); $csv_file->move(storage_path() . '/app', $tmp_import_name); //read csv $csv_data = []; if (($handle = fopen(storage_path() . '/app/' . $tmp_import_name, "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",", '"')) !== FALSE) { $csv_data[] = $data; } fclose($handle); } if (!empty($csv_data)) { //import erros holder $import_error_array = []; foreach ($csv_data as $k => $v) { if (is_array($v)) { $data_to_save = []; //set fields to be imported if (isset($v[0]) && !empty($v[0])) { $data_to_save['car_brand_name'] = trim($v[0]); } if (isset($v[1]) && !empty($v[1])) { $data_to_save['car_brand_active'] = trim($v[1]); } //check if all fields are here if (count($data_to_save) == 2) { try { CarBrand::create($data_to_save); } catch (\Exception $e) { $import_error_array[] = trans('admin_common.Possible doublicate') . '<strong>' . trans('admin_common.Car Brand') . '</strong>' . trans('admin_common.on line') . ': ' . join(',', $v) . ' <br />' . trans('admin_common.Error Message') . ': ' . $e->getMessage(); } } else { $import_error_array[] = trans('admin_common.Missing data line') . ': ' . join(',', $v); } } } } else { session()->flash('message', trans('admin_common.Cant read the csv file.')); return redirect(url('admin/carbrand')); } } /** * delete temp file, clear cache, set message, redirect to list */ @unlink(storage_path() . '/app/' . $tmp_import_name); Cache::flush(); if (!empty($import_error_array)) { session()->flash('message', trans('admin_common.Car Brands imported with the following errors') . ': <br />' . join('<br />', $import_error_array)); } else { session()->flash('message', trans('admin_common.Car Brands imported')); } return redirect(url('admin/carbrand')); } return view('admin.car_brand.car_brand_import'); }