Delete file and photo from folder using laravel 5: using larvel framework, you can remove unnecessary image or file with permanently delete from the destination folder. This tutorial will be helpful for you and you can apply the example code in your Laravel project.
You need to use set use File under namespace App; for file related work in your Laravel programming language. First you select the file value form the MySQL table, where you file name is insert. That’s like:
DB::table(‘Table name’)->where(‘id’,$id)->get();
Now store the image value in a variable, if you want to check the value is exist in the variable, so print the variable. For example:
$imgvalue=DB::table('recipe')->where('id',$id)->get();\ $media_name=$imgvalue[0]->media; echo $media_name;
Select the folder path and delete photo matching file value. Apply this code:
File::delete('uploads/images/' . $media_name);
If you want to delete records from the database and delete file from MySQL table with folder in the same time, you need to follow this example.
Example:
<?php namespace App; use Illuminate\Database\Eloquent\Model; use DB; use Session; use File; class AdminModel extends Model { public function Deletemedia($id){ $img=DB::table('recipe')->where('id',$id)->get(); $media_name=$img[0]->media; File::delete('uploads/images/' . $media_name); $data=DB::table('recipe')->where('id',$id)->delete(); if($data){ return true; }else{ return false; } } } ?>