muhammadmanssour / file-cast
轻松管理Laravel应用中的上传文件,自动保存路径并使用便捷的值对象轻松访问。
v0.3.1
2024-05-19 15:27 UTC
Requires
- php: ^8.2
- laravel/framework: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
README
此Laravel包使文件管理变得简单。它自动将上传的文件保存到磁盘并将它们的路径存储在数据库中。当你检索这些文件时,它会将这些路径包裹在易于使用的值对象中。该包还跟踪任何更改,确保在更新时删除旧文件。这是一个简单的方法,可以让你的文件处理保持整洁和高效。
功能
- 将上传的文件从请求映射到模型。
- 旧文件在模型更新或删除时自动删除。
- 文件值对象以方便访问
安装
您可以通过composer安装此包
composer require mohammedmanssour/file-cast
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="file-cast-config"
使用
- 将
FileCast
添加到您的casts列表中
protected $casts = [ 'government_id' => FileCast::class, // uses table name as storage path, public visibility and the configured default disk 'verification_video' => FileCast::class.':verification', // uses verification as storage path, public visisbility and the configured default disk 'profile_picture' => FileCast::class.':pics,,s3', // uses pics as storage path, and s3 as storage disk ];
- 可选,如果您想在模型更新或删除时清理旧文件,请将以下观察者添加到您的模型中
User::observe(UploadedFilesObserver::class);
- 在模型检索时,已映射的属性会被转换为文件对象,该对象提供了以下方法
$model->profile_picture->path(); // return the saved path in the db. $model->profile_picture->fullPath(); // return the full path based on the provided disk $model->profile_picture->size(); // return the file size $model->profile_picture->url(); // return the file url based on the provided disk $model->profile_picture->exists(); // return true of false $model->profile_picture->delete(); // delete the file
测试
composer test
变更日志
有关最近更改的更多信息,请参阅变更日志。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。