Exemplo n.º 1
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     $sanpham = Sanpham::where('id_loai', '=', $id)->count();
     if ($sanpham > 0) {
         return Redirect::to('loais')->with('errorDelete', "Thất bại.Loại này đang tồn tại sản phẩm");
     }
     $model = $this->loai->find($id);
     Logfileadmin::addData("Xóa", "Loại", $model->id, $model->tenloai);
     $this->loai->find($id)->delete();
     return Redirect::route('loais.index');
 }
 public function postSuaSanpham($id)
 {
     $input = Input::all();
     $validation = Validator::make($input, Sanpham::$rulesUpdate);
     if ($validation->passes()) {
         $image = Input::file('hinhanh');
         if (!is_null($image)) {
             // nếu có thay doi hinh anh
             $imageName = $image->getClientOriginalName();
             $nameArray = explode('.', $imageName);
             $imageType = end($nameArray);
             $imageRules = array("jpg", "jpeg", "png");
             if (in_array($imageType, $imageRules)) {
                 //neu dung thi tien hanh insert vao csdl
                 $imageNewName = uniqid(rand(), true);
                 $imageNewName = md5($imageNewName);
                 $imageNewName = substr($imageNewName, 0, 6);
                 $imageNewName .= "." . $imageType;
                 Input::file('hinhanh')->move('uploads', $imageNewName);
                 // upload hinh anh
                 //sau khi upload thi insert vao csdl
                 $sp = Sanpham::find($id);
                 $sp->tensp = Input::get('tensp');
                 $sp->donvitinh = Input::get('donvitinh');
                 $sp->hinhanh = $imageNewName;
                 $sp->chitietsp = Input::get('chitietsp');
                 $sp->khuyenmai = Input::get('khuyenmai');
                 $sp->id_loai = Input::get('id_loai');
                 //luu logfile
                 $info = Sanpham::where('id', '=', $id)->get()->toArray();
                 Logfileadmin::addData("Sửa", "Sản Phẩm", $sp->id, $sp->tensp, $info);
                 //hoan tat update
                 $sp->save();
                 return Redirect::to("sanpham/chitiet-sanpham/{$sp->id}");
             } else {
                 return Redirect::to("sanpham/sua-sanpham/{$id}")->with('errorImage', "Không đúng định dạng hình ảnh");
             }
         } else {
             //nếu không thay doi hinh anh
             $sp = Sanpham::find($id);
             $sp->tensp = Input::get('tensp');
             $sp->donvitinh = Input::get('donvitinh');
             $sp->chitietsp = Input::get('chitietsp');
             $sp->khuyenmai = Input::get('khuyenmai');
             $sp->id_loai = Input::get('id_loai');
             //luu logfile
             $info = Sanpham::where('id', '=', $id)->get()->toArray();
             Logfileadmin::addData("Sửa", "Sản Phẩm", $sp->id, $sp->tensp, $info);
             //hoan tat update
             $sp->save();
             return Redirect::to("sanpham/chitiet-sanpham/{$sp->id}");
         }
     } else {
         return Redirect::to("sanpham/sua-sanpham/{$id}")->withInput()->withErrors($validation)->with('message', 'There were validation errors.');
     }
 }