app/Http/Controllers/SanPhamController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        $sanpham= sanPham::findOrFail($id);
        $imageName = ($request->hinhanh)?$request->hinhanh->getClientOriginalName():false;
        if($imageName){
            $sanpham->hinhanh = $imageName;
            $request->validate([
                'hinhanh' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            ]);
            $request->hinhanh->move(public_path('images'), $imageName);
        }
        $request->validate([
            'gia' => 'min:0',
            'giakhuyenmai' => 'min:0'
        ]);
        $sanpham->tensanpham = $request->tensanpham;
        $sanpham->slug = $request->slug;
        $sanpham->gia = ($request->gia)?$request->gia:'0';
        $sanpham->giakhuyenmai = ($request->giakhuyenmai)?$request->giakhuyenmai:'0';
        $sanpham->mota = $request->mota;
        $sanpham->thongso = $request->thongso;
        $sanpham->baohanh = $request->baohanh;
        $sanpham->trangthai = ($request->trangthai='on')?true:false;
        $sanpham->id_nhasanxuat = $request->id_nhasanxuat;
        $sanpham->id_loaisanpham = $request->id_loaisanpham;
        $sanpham->save();
        return back()
            ->with('success','Sửa thành công')
            ->with('image',($imageName)?$imageName:'Không có thay đổi hình ảnh');
    }
public function update ( Request $ request , $ id ) { $ sanpham = sanPham :: findOrFail ( $ id ) ; $ imageName = ( $ request -> hinhanh ) ? $ request -> hinhanh -> getClientOriginalName ( ) : false ; if ( $ imageName ) { $ sanpham -> hinhanh = $ imageName ; $ request -> validate ( [ 'hinhanh' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048' , ] ) ; $ request -> hinhanh -> move ( public_path ( 'images' ) , $ imageName ) ; } $ request -> validate ( [ 'gia' => 'min:0' , 'giakhuyenmai' => 'min:0' ] ) ; $ sanpham -> tensanpham = $ request -> tensanpham ; $ sanpham -> slug = $ request -> slug ; $ sanpham -> gia = ( $ request -> gia ) ? $ request -> gia : '0' ; $ sanpham -> giakhuyenmai = ( $ request -> giakhuyenmai ) ? $ request -> giakhuyenmai : '0' ; $ sanpham -> mota = $ request -> mota ; $ sanpham -> thongso = $ request -> thongso ; $ sanpham -> baohanh = $ request -> baohanh ; $ sanpham -> trangthai = ( $ request -> trangthai = 'on' ) ? true : false ; $ sanpham -> id_nhasanxuat = $ request -> id_nhasanxuat ; $ sanpham -> id_loaisanpham = $ request -> id_loaisanpham ; $ sanpham -> save ( ) ; return back ( ) -> with ( 'success' , 'Sửa thành công') -> with ( 'image' , ( $ imageName ) ? $ imageName : 'Không có thay đổi hình ảnh'); }
Update the specified resource in storage.
Update the specified resource in storage .
// //