Language

How To Delete a File (Photo) From Both Folder and Database Laravel 5.4

Date 13 May, 2017

Details :

1. If you want to delete a File or Image from folder (directory) when deleting it from Database

     you just need to call File::delete($pathToFile);

2. For Example:

   If you need to delete a specific File or Image just call File Facade as shown below

public function deleteposts($id)
   {
       $image =Post::where('id',$id)->first();
       $image  =  json_decode( json_encode($image), true);

       $filename= $image['image'];                                     // Get Filename from database

       $filepath = public_path().'/images/'.$filename;        // Get Full Filepath to the file

       File::delete($filepath);                                             // Now Delete File using filepath  

       DB::table('posts')->where('id', $id)->delete();         // Get Delete File entry from database
             
       return Redirect::back();                     
   }

Note: Don't Forget to Write   use File;   at the topside of your controller