app/Http/Controllers/BrandController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Brand  $brand
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        $validated = $request->validate([
                'brand_name' => 'required|min:4',
            ],
            [
                'brand_name.required' => 'Please Input Brand Name',
                'brand_image.min' => 'Brand longer than 4 Characters',
            ]
        );
        $old_image = $request->old_image;
        $brand_image = $request->file('brand_image');
        if($brand_image){
            $name_gen = hexdec(uniqid());
            $img_ext = strtolower($brand_image->getClientOriginalExtension());
            $img_name = $name_gen.'.'.$img_ext;
            $up_location = 'image/brand/';
            $last_img = $up_location.$img_name;
            $brand_image->move($up_location,$img_name);
            unlink($old_image);
            Brand::find($id)->update([
                'brand_name' => $request->brand_name,
                'brand_image' => $last_img,
                'created_at' => Carbon::now()
            ]);
        }else{
            Brand::find($id)->update([
                'brand_name' => $request->brand_name,
                'created_at' => Carbon::now()
            ]);
        }
        return redirect()->back()->with('success', 'Category Inserted Succesfully!');
    }
public function update ( Request $ request , $ id ) { $ validated = $ request -> validate ( [ 'brand_name' => 'required|min:4' , ] , [ 'brand_name.required' => 'Please Input Brand Name' , 'brand_image.min' => 'Brand longer than 4 Characters' , ] ) ; $ old_image = $ request -> old_image ; $ brand_image = $ request -> file ( 'brand_image' ) ; if ( $ brand_image ) { $ name_gen = hexdec ( uniqid ( ) ) ; $ img_ext = strtolower ( $ brand_image -> getClientOriginalExtension ( ) ) ; $ img_name = $ name_gen . '.' . $ img_ext ; $ up_location = 'image/brand/' ; $ last_img = $ up_location . $ img_name ; $ brand_image -> move ( $ up_location , $ img_name ) ; unlink ( $ old_image ) ; Brand :: find ( $ id ) -> update ( [ 'brand_name' => $ request -> brand_name , 'brand_image' => $ last_img , 'created_at' => Carbon :: now ( ) ] ) ; } else { Brand :: find ( $ id ) -> update ( [ 'brand_name' => $ request -> brand_name , 'created_at' => Carbon :: now ( ) ] ) ; } return redirect ( ) -> back ( ) -> with ( 'success' , 'Category Inserted Succesfully!' ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
/**
         * as category_name is a field name
         * unique:categories, categories as table name
         * */ // for replacing the image // image name generator //upload image