app/Http/Controllers/Admin/FileUploadController.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)
    {
        $request->validate([
            'title' => 'required|max:250',
            'file' => 'nullable|file',
        ]);
        if($request->hasFile('file')){
            $old_file = FileUpload::find($id);
            $file_path = base_path($old_file->file_path);
            if(File::isFile($file_path)){
                File::delete($file_path);
            }
            $filenameWithExt = $request->file('file')->getClientOriginalName();
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME); 
            $extension = $request->file('file')->getClientOriginalExtension();
            $fileNameToStore = $filename.'.'.$extension;
            $path = base_path();
            if (! File::exists($path)) {
                File::makeDirectory($path, 0777, true, true);
            }
            $file = $request->file('file')->move($path, $fileNameToStore);
        }
        else{
            $old_file = FileUpload::find($id);
            $fileNameToStore = $old_file->file_path; 
        }
        $data = FileUpload::find($id);
        $data->title = $request->title;
        $data->file_path = $fileNameToStore;
        $data->updated_by = Auth::user()->id;
        $data->save();
        Toastr::success($this->title.' Updated Successfully!', 'success');
        return redirect()->back();
    }
public function update ( Request $ request , $ id ) { $ request -> validate ( [ 'title' => 'required|max:250' , 'file' => 'nullable|file' , ] ) ; if ( $ request -> hasFile ( 'file' ) ) { $ old_file = FileUpload :: find ( $ id ) ; $ file_path = base_path ( $ old_file -> file_path ) ; if ( File :: isFile ( $ file_path ) ) { File :: delete ( $ file_path ) ; } $ filenameWithExt = $ request -> file ( 'file' ) -> getClientOriginalName ( ) ; $ filename = pathinfo ( $ filenameWithExt , PATHINFO_FILENAME ) ; $ extension = $ request -> file ( 'file' ) -> getClientOriginalExtension ( ) ; $ fileNameToStore = $ filename . '.' . $ extension ; $ path = base_path ( ) ; if ( ! File :: exists ( $ path ) ) { File :: makeDirectory ( $ path , 0777 , true , true ) ; } $ file = $ request -> file ( 'file' ) -> move ( $ path , $ fileNameToStore ) ; } else { $ old_file = FileUpload :: find ( $ id ) ; $ fileNameToStore = $ old_file -> file_path ; } $ data = FileUpload :: find ( $ id ) ; $ data -> title = $ request -> title ; $ data -> file_path = $ fileNameToStore ; $ data -> updated_by = Auth :: user ( ) -> id ; $ data -> save ( ) ; Toastr :: success ( $ this -> title . ' Updated Successfully!' , 'success' ) ; return redirect ( ) -> back ( ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
// Field Validation // file upload, fit and store inside public folder  //Delete Old File //Upload New File //Crete Folder Location // Move File inside public/uploads/ folder // Update Data