validate([ 's' => 'nullable|string', 's_series' => 'nullable|string', 's_price_min' => 'nullable|integer', 's_price_max' => 'nullable|integer', 'perpage' => 'nullable|string', ]); $query = Product::query(); if (!empty($data['s'])) { $query->whereRaw(" (`article` LIKE '%{$data['s']}%' OR `name` LIKE '%{$data['s']}%' OR `name_for_form` LIKE '%{$data['s']}%' OR `product_group` LIKE '%{$data['s']}%' OR `characteristics` LIKE '%{$data['s']}%' OR `tech_description` LIKE '%{$data['s']}%' OR `tech_description_short` LIKE '%{$data['s']}%') "); } if (!empty($data['s_series'])) { $query->where('series', '=', $data['s_series']); } if (!empty($data['s_price_min'])) { $query->where('price', '>=', $data['s_price_min']); } if (!empty($data['s_price_max'])) { $query->where('price', '<=', $data['s_price_max']); } $query->orderBy('series'); $data['products'] = $query->paginate($data['perpage'] ?? 20)->withQueryString(); // get all series for select field $data['series'] = Product::query()->select('series')->distinct()->OrderBy('series')->get(); return view('products.index', $data); } // todo вынести в job, чтобы работала в фоне public function upload_xls(Request $request) { $xls = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($request->file('file')); $xls->setReadDataOnly(true); $sheet = $xls->load($request->file('file')); // read xls to array $goods = $sheet->setActiveSheetIndex(0)->toArray(); unset($sheet, $xls); $series = ''; $i = 0; $created = 0; $updated = 0; $no_image = []; foreach ($goods as $good) { // check first line and skip it if ($good[0] === '№п/п' && $good['3'] === 'Наименование') continue; // check the line is name of series if ($good[0] == NULL && $good[1] == NULL && $good[2] == NULL && is_string($good[3])) { $series = $good[3]; continue; } $tmp = explode("\n", $good[3]); if (!isset($tmp[1])) { $good[3] = preg_replace('!\s+!', ' ', $good[3]); $tmp = explode(' ', $good[3], 2); } $images = $this->find_images($tmp[0]); $data = [ // 'article' => $tmp[0], 'series' => $series, 'name' => (isset($tmp[1])) ? $tmp[1] : 'error', 'name_for_form' => $good[2], 'product_group' => $good[1], 'price' => $good[5], 'characteristics' => $good[4], 'tech_description' => $good[7], 'tech_description_short' => $good[8], 'image_path' => (!empty($images)) ? $images[0] : $images, ]; $a = Product::query()->updateOrCreate(['article' => $tmp[0]], $data); if ($a->wasRecentlyCreated) $created++; else $updated++; //echo $i++ . '. Серия: ' . $series . ', артикул: ' . $tmp[0] . '
'; $i++; if ($data['image_path'] == '') $no_image[] = $tmp[0]; } return view('products.import_result', ['count' => $i, 'updated' => $updated, 'created' => $created, 'no_image' => $no_image]); } private $allfiles; // remember files list private function find_images($article) { $path_to_files = './' . env('IMAGES_PATH', '---') . '/'; if (!isset($this->allfiles) || empty($this->allfiles)) { $this->allfiles = scandir($path_to_files); } $images = []; foreach ($this->allfiles as $filename) { if ((mb_strpos($filename, $article) === 0) || ( mb_strpos(Str::lower($filename), Str::slug($article)) === 0)) $images[] = $filename; } return (!empty($images)) ? $images : ''; } public function product(Request $request, $id) { $data['product'] = Product::query()->findOrFail($id); $data['images'] = $this->find_images($data['product']->article); return view('products.product', $data); } public function save_product(SaveProductRequest $request) { Product::query()->where('id', $request->validated('id'))->update($request->validated()); return redirect()->route('index'); } public function upload_image(Request $request){ $path = public_path() . '/' . env('IMAGES_PATH'); // dd($path); $file = $request->file('filename'); $new_filename = $request->article . '.' . date('YmdHis') . '.' . $file->extension(); $file->move($path, $new_filename); Product::query()->where('article', $request->article)->update(['image_path' => $new_filename]); return redirect()->back(); } public function update_image(Request $request, $id){ $validated = $request->validate(['image_path' => 'required|string']); Product::query()->where('id', $id)->update($validated); return redirect()->route('view_product', $id); } public function select_export(Request $request){ $data['ids'] = json_decode($request->ids, true); $data['products'] = Product::query()->whereIn('id', $data['ids'])->get(); return view('products.select_export', $data); } }