app/Http/Controllers/GalleriController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Galleri  $galleri
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request,$id)
    {
        $this->validate($request,[
            'photos' => 'required|',
        ]);
        $galleri = Galleri::findOrFail($id);
        if ($request->hasFile('photos')){ 
            $file = $request->file('photos');
            $destinationPath = public_path() .DIRECTORY_SEPARATOR. 'img';
            $filename = str_random(6).'_'.$file->getClientOriginalName();
            $uploadSuccess = $file->move($destinationPath, $filename);
            if($galleri->photos){
                $old_photo = $galleri->photos;
                $filepath = public_path() .DIRECTORY_SEPARATOR. '/img' .DIRECTORY_SEPARATOR. $galleri->photos;
                    try{
                        File::delete($filepath);
                    }catch(FileNotFoundException $e){
                    }
            }
            $galleri->photos = $filename;
        }
            $galleri->save();
            return redirect()->route('galleri.index');
    }
public function update ( Request $ request , $ id ) { $ this -> validate ( $ request , [ 'photos' => 'required|' , ] ) ; $ galleri = Galleri :: findOrFail ( $ id ) ; if ( $ request -> hasFile ( 'photos' ) ) { $ file = $ request -> file ( 'photos' ) ; $ destinationPath = public_path ( ) . DIRECTORY_SEPARATOR . 'img' ; $ filename = str_random ( 6 ) . '_' . $ file -> getClientOriginalName ( ) ; $ uploadSuccess = $ file -> move ( $ destinationPath , $ filename ) ; if ( $ galleri -> photos ) { $ old_photo = $ galleri -> photos ; $ filepath = public_path ( ) . DIRECTORY_SEPARATOR . '/img' . DIRECTORY_SEPARATOR . $ galleri -> photos ; try { File :: delete ( $ filepath ) ; } catch ( FileNotFoundException $ e ) { } } $ galleri -> photos = $ filename ; } $ galleri -> save ( ) ; return redirect ( ) -> route ( 'galleri.index' ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
//hapus foto lama