作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经创建了一个注册表。我创建了一个表来显示数据库中的所有数据。还有一个名为“删除”的按钮,我已经传递了唯一的 id。当我单击“删除”按钮时,它会从数据库中删除数据。但是,我想从数据库和文件夹中删除文件或图像。我的文件和图像存储在公共(public)文件夹中。
我该如何解决这个问题?
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
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');
最佳答案
我可以提出一个解决方案,即:设置查询生成器来查找文件:
$query = DB::table('academic')->where('id',$id);
假设 file_path 是一个在表中保存路径的字段
$files_to_delete = $query->pluck('file_path')->toArray(); //keeping the result in a php
$query->delete(); //now deleting
Storage::delete($files_to_delete);
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”目录来查找该文件。
关于php - 如何在 Laravel 中从文件夹和数据库中删除文件或图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45551170/
我是一名优秀的程序员,十分优秀!