public function run() { $folderpath = 'database/seeds/seed_files'; $folders = File::directories($folderpath); $latest = '11232015'; foreach ($folders as $value) { $_dir = explode("/", $value); $cnt = count($_dir); $name = $_dir[$cnt - 1]; $latest_date = DateTime::createFromFormat('mdY', $latest); $now = DateTime::createFromFormat('mdY', $name); if ($now > $latest_date) { $latest = $name; } } $file_path = $folderpath . "/" . $latest . "/Secondary Display.xlsx"; echo (string) $file_path, "\n"; Model::unguard(); DB::statement('SET FOREIGN_KEY_CHECKS=0;'); DB::table('secondary_displays')->truncate(); $reader = ReaderFactory::create(Type::XLSX); // for XLSX files $filePath = $file_path; $reader->open($filePath); // Accessing the sheet name when reading foreach ($reader->getSheetIterator() as $sheet) { if ($sheet->getName() == 'Sheet2') { $cnt = 0; foreach ($sheet->getRowIterator() as $row) { if (!is_null($row[0])) { if ($cnt > 0) { $category = FormCategory::firstOrCreate(['category' => strtoupper($row[0])]); $category->secondary_display = 1; $category->update(); if (!empty($category)) { SecondaryDisplay::create(array('category_id' => $category->id, 'brand' => $row[1])); } } $cnt++; } } } else { } } $reader->close(); DB::statement('SET FOREIGN_KEY_CHECKS=1;'); Model::reguard(); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, ['brand' => 'required|max:100|unique_with:secondary_displays, category = category_id', 'category' => 'required|not_in:0']); \DB::beginTransaction(); try { $display = new SecondaryDisplay(); $display->category_id = $request->category; $display->brand = $request->brand; $display->save(); \DB::commit(); Session::flash('flash_message', 'Secondary Display successfully added!'); return redirect()->route("secondarydisplay.index"); } catch (Exception $e) { DB::rollBack(); return redirect()->back(); } }