abdelrahmanbl / laravel-uploadable
此包为模型添加自我上传图片(如头像)或其他文件类型的特性。
Requires (Dev)
- orchestra/testbench: ^7.0|^8.0|^9.0
- pestphp/pest: ^1.20|^2.0
README
Laravel Uploadable 为模型添加自我上传文件(如头像或任何文件类型)的行为。
简介
此包可以帮助您将图片或任何类型的文件上传到文件系统中的特定位置,您可以为表中的每个字段指定一个目录以保存上传的文件,或者您可以使用包的默认存储目录。
安装
composer require abdelrahmanbl/laravel-uploadable
关于上传
此包使用 Laravel 文件存储 来管理文件。文件将存储在默认磁盘上。例如,如果您使用公共磁盘,要访问图片或文件,您需要在项目中创建一个符号链接
php artisan storage:link
然后,从 .env 文件配置您的默认文件系统
APP_URL=https://your-domain.com
FILESYSTEM_DISK=public # or your prefered disk
您可以将 default_url
添加到文件系统配置文件中,以覆盖默认文件 URL。默认文件 URL 是 asset('uploadable.jpg')。
使用方法
要使用此包,请在模型中导入 FileCast,然后使用 FileCast 类配置模型的 $casts。
use Bl\LaravelUploadable\Casts\FileCast;
class User extends model
{
/**
* Don't forget
* Add all the fields for file or image to the model $fillable when you don't use model $guarded.
*
* @var array
*/
protected $fillable = [
...,
'avatar',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
...,
'avatar' => FileCast::class,
];
}
自定义目录
'avatar' => FileCast::class . ':User/avatar', # this is the default value ( the attribute key name inside the model basename )
自定义磁盘
'avatar' => FileCast::class . ':default,s3', # here we customized the disk to s3.
自定义驱动程序
'avatar' => FileCast::class . ':default,default,' . CustomDriverService::class,
注意:您的自定义驱动程序服务必须实现
Bl\LaravelUploadable\Interfaces\UploadFileInterface
并具有一个接受参数 $disk 的构造函数。
use Bl\LaravelUploadable\Interfaces\UploadFileInterface;
use Illuminate\Http\UploadedFile;
class CustomDriverService implements UploadFileInterface
{
protected $disk;
/**
* __construct
*
* @param \Bl\LaravelUploadable\Classes\FileArgument $disk
* @return void
*/
public function __construct($disk)
{
$this->disk = $disk->getValue();
}
/**
* handle store proccess of the file.
*
* @param UploadedFile $file
* @param string $directory
* @return mixed
*/
public function store(UploadedFile $file, string $directory): mixed
{
// ...
}
/**
* handle getting the file full url path.
*
* @param string $path
* @return string
*/
public function get(string $path): mixed
{
// ...
}
/**
* handle deleting a file.
*
* @param string $path
* @return void
*/
public function delete(string $path): void
{
// ...
}
}
自定义默认路径
'avatar' => FileCast::class . ':default,default,default,null', # customize the default value to null.
'avatar' => FileCast::class . ':default,default,default,nullable', # customize the default value to null.
'avatar' => FileCast::class . ':default,default,default,avatar.png', # customize the default value to asset('avatar.png').
这就是全部!配置完成后,您可以从客户端发送与模型中每个文件字段同名的文件数据。此包将完成魔法!
示例
在前端,您可以创建一个具有字段名 avatar 的表单数据。
<form method="POST" action="" enctype="multipart/form-data">
@csrf
<div>
<label for="file">File</label>
<input type="file" name="avatar" id="file">
</div>
<div>
<button>
Submit
</button>
</div>
</form>
在后端,您可以传递所有数据到 User 模型。
/**
* Handle the incoming request.
*/
public function store(UploadRequest $request)
{
$user = \App\Models\User::query()->create(
$request->validated() // or you can use $request->all() if you don't make a validation
);
// this get a link of the image that uploaded.
$user->avatar; # https://domain.com/storage/User/avatar/U4q6En4mOHMJj0.png
}
您可以手动更新文件到 User 模型。
/**
* Handle the incoming request.
*/
public function store(UploadRequest $request)
{
$user = \App\Models\User::query()->first();
$user->avatar = $request->file('avatar');
$user->save();
// this get a link of the image that uploaded.
$user->avatar; # https://domain.com/storage/User/avatar/U4q6En4mOHMJj0.png
}
注意:当更新具有文件的字段时,包将自动删除旧文件并放置新文件。
删除文件
您可以在模型中使用 FileCastRemover 特性,当删除模型实例时,所有相关文件将自动删除。
use Bl\LaravelUploadable\Traits\FileCastRemover;
class User extends Model
{
use FileCastRemover;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'avatar',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'avatar' => FileCast::class,
];
}
模型实例删除后,所有相关文件都将被移除。
/**
* Remove the user.
*/
public function destroy(User $user)
{
$user->delete();
}
应用事件
您可以在文件上传前或后应用事件。此外,您可以为自定义字段应用这些事件。
全局事件
- 要在文件上传前应用全局事件,应在模型中定义一个名为
beforeFileCastUpload
的方法,它接受一个类型为\Illuminate\Http\UploadedFile
的参数并返回相同类型。 - 要在文件上传后应用全局事件,应在模型中定义一个名为
afterFileCastUpload
的空方法,它接受一个类型为\Illuminate\Http\UploadedFile
的参数。
use Illuminate\Http\UploadedFile;
class User extends model
{
/**
* Apply before the file cast upload event.
*
* @param UploadedFile $file
* @return UploadedFile
*/
public function beforeFileCastUpload(UploadedFile $file): UploadedFile
{
return $file;
}
/**
* Apply after the file cast upload event.
*
* @param UploadedFile $file
* @return void
*/
public function afterFileCastUpload(UploadedFile $file): void
{
dd($file);
}
}
自定义事件
要应用自定义事件,应创建一个实现 Bl\LaravelUploadable\Interfaces\EventUploadInterface
的服务,并将其作为参数传递。
'avatar' => FileCast::class . ':default,default,default,default,' . CustomUploadService::class
use Bl\LaravelUploadable\Interfaces\EventUploadInterface;
use Illuminate\Http\UploadedFile;
class CustomUploadService implements EventUploadInterface
{
/**
* Apply before the file cast upload event.
*
* @param UploadedFile $file
* @return UploadedFile
*/
public function before(UploadedFile $file): UploadedFile
{
return $file;
}
/**
* Apply after the file cast upload event.
*
* @param UploadedFile $file
* @return void
*/
public function after(UploadedFile $file): void
{
dd($file);
}
}
注意:在模型中应用全局和自定义事件时,优先级在自定义事件。
贡献
请随时评论、提出问题或发送 PR。祝您享受其中!!
许可证
MIT 许可证。有关更多信息,请参阅许可证文件。