问题
database
id. project_id. type. images
1. 23. frontend 3344.png
2. 23. frontend. 3326.png
3. 23. er diagram 4290.png
4. 23 wireframe. 995.png
5. 23. wireframe. 8875.png
view blade
@foreach($project_data as $project)
<td>
<a title="delete" href="javascript:void(0);" class="confirmDelete" name="project_data" record="delete_project" recordid="{{$project['id']}}" style="color: red"><i class="fas fa-trash"></i></a>
</td>
@endforeach
route
Route::get('delete_project/{id}','ProjectDataController@deleteProject');
controller
public function deleteProject($id){
$project_items = ProjectImages::select('images')->where('project_id',$id)->first();
$project_image_path_front = 'hash images/project images/frontend/';
$project_image_path_er = 'hash images/project images/er diagram/';
$project_image_path_wire = 'hash images/project images/wireframe/';
if(file_exists($project_image_path_front.$project_items->images)){
unlink($project_image_path_front.$project_items->images);
}
if(file_exists($project_image_path_er.$project_items->images)){
unlink($project_image_path_er.$project_items->images);
}
if(file_exists($project_image_path_wire.$project_items->images)){
unlink($project_image_path_wire.$project_items->images);
}
ProjectImages::where('project_id',$id)->delete();
}
how to delete the image from the database and from its path also this code only delete the first image which is mentioned in the database
来源:https://stackoverflow.com/questions/64920122/php-laravel-how-to-delete-multi-images-with-its-path-in-laravel