/** * Run the database seeds. * * @return void */ public function run() { function csv_to_array($filename = '', $delimiter = ',') { if (!file_exists($filename) || !is_readable($filename)) { return FALSE; } $header = NULL; $data = array(); if (($handle = fopen($filename, 'r')) !== FALSE) { while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) { if (!$header) { $header = $row; } else { $data[] = array_combine($header, $row); } } fclose($handle); } return $data; } $csvFile = public_path() . '/csvs/categories.csv'; $datas = csv_to_array($csvFile); DB::table('categories')->delete(); foreach ($datas as $data) { Categories::create($data); } }
/** * Update the specified resource in storage. * * @param Request $request * @param int $id * @return Response */ public function update(Request $request, $id) { $category = Categories::find($id); $parameters = $request->except(['_token']); $validator = Validator::make($request->all(), ['name' => 'required|unique:foods|max:255']); if ($validator->fails()) { return redirect()->route('food.create')->withErrors($validator)->withInput(); } if (empty($parameters['slug'])) { $parameters['slug'] = Str::slug($parameters['name']); } $category->name = $parameters['name']; $category->slug = $parameters['slug']; $category->save(); return redirect()->route('category.index')->with('success', 'Category was updated !'); }