/** * Update the specified resource in storage. * * @param DesignRequest $request * @param Design $design * @return Response */ public function update(DesignRequest $request, Design $design) { $data = $request->all(); $data['images'] = $design->images ? $design->images : []; $i = 0; $files = json_decode($data['files_deleted']); foreach ($files as $file) { if (($key = array_search($file, $data['images'])) !== false) { unset($data['images'][$key]); $this->delete($file); } } foreach ($_FILES['images']['tmp_name'] as $tmpPath) { if (!empty($tmpPath)) { $fileUpload = $this->upload($tmpPath, $i++); array_push($data['images'], $fileUpload); } } // Hàm unset() khiến key của array ko còn là dãy số liên tiếp // Lúc này Laravel sẽ ko đối xử và lưu 'images' như kiểu array mà là kiểu Json, cần sửa chữa vấn đề này $data['images'] = array_values($data['images']); $design->fill($data)->save(); return redirect('quan-tri/thiet-ke-thi-cong/create')->with('flash_message', Lang::get('system.update')); }