如何从Laravel的文件夹和数据库中删除文件或图像? [英] How To Delete Files Or Images From Folder And Database In Laravel?

查看:206
本文介绍了如何从Laravel的文件夹和数据库中删除文件或图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个注册表.我创建了一个表来显示数据库中的所有数据.还有一个名为删除"的按钮,我已经传递了唯一的ID.当我单击删除"按钮时,它将从数据库中删除数据.但是,我想从数据库和文件夹中删除文件或图像. 我的文件和图像存储在公用文件夹中.

I have created a Registration form. And I have created a Table to show all the data which is in Database. And there is a button called Delete and I have passed the unique id. When I click that Delete button , it deletes the data from Database. But , I want to Delete Files or Images from Database and Folder. My Files and Images stored in the public folder.

如何解决此问题?

删除按钮代码. (RegView.blade.php)

Delete button code. ( RegView.blade.php )

@foreach($data as $value )
<tr>
<td> <a href="delete{{ $value->id }}"><input type="submit" name="delete" value="Delete" class="btn-danger"></a> </td>
</tr>
@endforeach

控制器代码. (RegViewController.blade.php)

Code for controller. ( RegViewController.blade.php )

 function delete($id)
    {
        DB::table('academic')->where('id',$id)->delete();
        return redirect('RegView');
    }

这是我创建的路线.

Here is the Routes that I have created.

Route::get('delete{id}','RegViewController@delete');

推荐答案

我可以提出一个解决方案,即:设置查询生成器以查找文件:

I can propose a solution, i.e: setup the query builder to find the file:

$query = DB::table('academic')->where('id',$id);

假设file_path是一个将路径保留在表中的字段

Assuming the file_path is a field that keeps the path in your table

$files_to_delete = $query->pluck('file_path')->toArray(); //keeping the result in a php 
$query->delete(); //now deleting
Storage::delete($files_to_delete);

PS:您可以在 Laravel文档,并且文档中只有一个重要信息如下:

PS: You can read more about similar methods with Storage in Laravel docs and only one important info in the doc is the following:

表示它将在"storage/app"目录中查找该文件.

Meaning that it looks for 'storage/app' directory to find that file.

这篇关于如何从Laravel的文件夹和数据库中删除文件或图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆