public function update(MainCategory $main_category) { $rules = array('main_cat_title' => 'required|string'); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { //TODO add error messages die("validator failed"); } else { //first delete categories $delete_sub_cat_ds = Input::get("delete_sub_categories"); if ($delete_sub_cat_ds) { $delete_sub_cat_ds_array = explode(",", $delete_sub_cat_ds); $sub_cat = SubCategory::where("main_category_id", "=", $main_category->id)->whereIn("id", $delete_sub_cat_ds_array); $sub_cat->delete(); } //then add new ones $new_sub_categories = Input::get('sub_category_new_titles'); if ($new_sub_categories != "") { $sub_categories_aray = explode(",", $new_sub_categories); $subCats = array(); foreach ($sub_categories_aray as $item) { $subCats[] = new SubCategory(array("title" => $item)); } $main_category->sub_categories()->saveMany($subCats); } $main_category->title = Input::get('main_cat_title'); $main_category->save(); return redirect('cms/product/category'); } }
protected function findModel($id) { if (($model = SubCategory::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function beforeDelete() { if (parent::beforeDelete()) { SubCategory::deleteAll(['cate_id' => $this->id]); return true; } else { return false; } }
/** * Remove the specified subCategory from storage. * * @param DeleteInventoryRequest $request * * @param SubCategory $subCategory * @return Response * @throws \Exception */ public function destroy(DeleteInventoryRequest $request, SubCategory $subCategory) { $this->data = $subCategory->delete(); return $this->handleRedirect($request, route('backend.subcategories.index')); }
public function run() { Model::unguard(); DB::statement('SET FOREIGN_KEY_CHECKS=0;'); $folderpath = base_path() . '/database/seeds/seed_files/'; $folders = File::directories($folderpath); $latest = '11232015'; foreach ($folders as $value) { $_dir = explode("/", str_replace('\\', '/', $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; } } $filePath = $folderpath . $latest . '/Masterfile.xlsx'; $reader = ReaderFactory::create(Type::XLSX); // for XLSX files $reader->open($filePath); // DB::table('divisions')->truncate(); // DB::table('categories')->truncate(); // DB::table('sub_categories')->truncate(); // DB::table('brands')->truncate(); // DB::table('items')->truncate(); foreach ($reader->getSheetIterator() as $sheet) { if ($sheet->getName() == 'Items') { $cnt = 0; Item::where('active', 1)->update(['active' => 0, 'cleared' => 0]); foreach ($sheet->getRowIterator() as $row) { if ($row[0] != '') { if ($cnt > 0) { $division = Division::firstOrCreate(['division' => strtoupper($row[9])]); $category = Category::firstOrCreate(['category' => strtoupper($row[1]), 'category_long' => strtoupper($row[0])]); $sub_category = SubCategory::firstOrCreate(['category_id' => $category->id, 'sub_category' => strtoupper($row[7])]); $brand = Brand::firstOrCreate(['brand' => strtoupper($row[8])]); $itemExist = Item::where('sku_code', strtoupper($row[2]))->first(); if (empty($itemExist)) { $item = Item::firstOrCreate(['sku_code' => trim($row[2]), 'barcode' => $row[3], 'description' => strtoupper($row[4]), 'description_long' => strtoupper($row[5]), 'conversion' => trim($row[6]), 'lpbt' => trim($row[10]), 'division_id' => $division->id, 'category_id' => $category->id, 'sub_category_id' => $sub_category->id, 'brand_id' => $brand->id, 'active' => 1]); } else { $itemExist->sku_code = trim($row[2]); $itemExist->barcode = $row[3]; $itemExist->description = strtoupper($row[4]); $itemExist->description_long = strtoupper($row[5]); $itemExist->conversion = trim($row[6]); $itemExist->lpbt = trim($row[10]); $itemExist->division_id = $division->id; $itemExist->category_id = $category->id; $itemExist->sub_category_id = $sub_category->id; $itemExist->brand_id = $brand->id; $itemExist->active = 1; $itemExist->save(); } } $cnt++; } } } } $reader->close(); DB::statement('SET FOREIGN_KEY_CHECKS=1;'); Model::reguard(); }
public function getSubcategory() { return $this->hasOne(SubCategory::className(), ['id' => 'scate_id']); }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $this->validate($request, ['sku_code' => 'required', 'description' => 'required', 'conversion' => 'required|numeric', 'lpbt' => 'required|numeric', 'division' => 'required', 'category' => 'required', 'sub_category' => 'required', 'brand_id' => 'required']); $item = Item::findOrFail($id); $divname = $request->division; $divid = Division::where('division', '=', $divname)->first(); $divname = $divid->id; $catname = $request->category; $catid = Category::where('category', '=', $catname)->first(); $catname = $catid->id; $scatname = $request->sub_category; $scatid = SubCategory::where('sub_category', '=', $scatname)->first(); $scatname = $scatid->id; $item->sku_code = $request->sku_code; $item->description = $request->description; $item->conversion = $request->conversion; $item->lpbt = $request->lpbt; $item->division_id = $divname; $item->category_id = $catname; $item->sub_category_id = $scatname; $item->brand_id = $request->brand_id; $item->description_long = $request->description_long; $item->barcode = $request->barcode; $item->active = $request->status; $item->update(); Session::flash('flash_class', 'alert-success'); Session::flash('flash_message', 'Item successfully updated.'); return redirect()->route("item.index"); }