app/Http/Controllers/CategoryController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, Category $category)
    {
        $request->validate([
            'name' => 'required'
        ]);
        if($request->file()){
            $fileName = rand().'_'.$request->photo->getClientOriginalName();
            $filePath = $request->file('photo')->storeAs('categoryimg',$fileName,'public');
            $path = '/storage/'.$filePath;
        }else{
            $path = $category->photo;
        }
        $category->name = $request->name;
        $category->photo = $path;
        $category->save();
        return redirect()->route('categories.index');
    }
public function update ( Request $ request , Category $ category ) { $ request -> validate ( [ 'name' => 'required' ] ) ; if ( $ request -> file ( ) ) { $ fileName = rand ( ) . '_' . $ request -> photo -> getClientOriginalName ( ) ; $ filePath = $ request -> file ( 'photo' ) -> storeAs ( 'categoryimg' , $ fileName , 'public' ) ; $ path = '/storage/' . $ filePath ; } else { $ path = $ category -> photo ; } $ category -> name = $ request -> name ; $ category -> photo = $ path ; $ category -> save ( ) ; return redirect ( ) -> route ( 'categories.index' ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
// dd($request); // validation // upload file // 1665003670_290012.png // categoryimg/1665003670_290012.png // delete old photo // if(){ //     Storage::delete('/public/categoryimg/'.); // } // data update // redirect