dongttfd / laravel-upload-model
Laravel 模型上传
0.2.7
2024-09-25 07:15 UTC
Requires
- php: >=7.0,<8.5
- illuminate/config: >=5.7,<12.0
- illuminate/database: >=5.7,<12.0
- illuminate/filesystem: >=5.7,<12.0
- illuminate/http: >=5.7,<12.0
Requires (Dev)
- league/flysystem-aws-s3-v3: >=1.0,<5.0
- orchestra/testbench: >=4.0,<10.0
This package is auto-updated.
Last update: 2024-09-25 07:22:24 UTC
README
Laravel 支持
"laravel/framework": "^5.7|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0"
功能描述
- 通过模型函数(例如:
create, update, fill, save ...
)轻松快速地将上传的文件移动到您的文件夹 - 方便自动分配文件路径到Eloquent模型属性
- 轻松重试模型文件
- 集成JSON列
- 集成软删除
安装
通过Composer
composer require dongttfd/laravel-upload-model
基本用法
在模型中使用包
namespace App\Models; use DongttFd\LaravelUploadModel\Contracts\UploadOnEloquentModel; use DongttFd\LaravelUploadModel\Eloquent\UploadFileEloquent; class User extends Model implements UploadOnEloquentModel { use UploadFileEloquent; ... }
实现
使用上传文件创建模型
... // $file must is instance of Illuminate\Http\UploadedFile $file = $request->file('avatar'); User::create(['avatar' => $file]);
使用上传文件更新模型
... // $file must is instance of Illuminate\Http\UploadedFile $file = $request->file('avatar'); $user->update(['avatar' => $file]);
易于重试
... // $file must is instance of Illuminate\Http\UploadedFile $file = $request->file('avatar'); $user->update(['avatar' => $file]); // path of file $user->avatar; // url of file $user->avatar_url;
支持的JSON列
在使用JSON列之前请参阅覆盖
带有JSON/Array列
// JSON object $front = $request->file('front'); $back = $request->file('back'); $user->create([ ... 'card' => [ 'front' => $front, 'back' => $back ] ]); // retries path of file $user->card['front']; $user->card['back']; // retries url of file $user->card['front_url']; $user->card['back_url'];
// JSON Array $photos = $request->file('photos'); // [UploadedFile, UploadedFile] $post->create([ ... 'photos' => $photos ]); // retries path of file $post->photos; // ['<photo1-path>', <photo2-path>] // retries url of file $post->photos_url; // ['<photo1-url>', <photo2-url>]
// JSON Array and Object combined $variant = $request->only([ 'photos', // UploadedFile[] 'name', // String 'key' // String ]); $post->update([ ... 'variant' => $variant ]); // retries path of file $post->variant['photos']; // ['<photo1-path>', <photo2-path>] // retries url of file $post->variant['photos_url']; // ['<photo1-url>', <photo2-url>]
覆盖
我们建议在您的Laravel项目中创建一个BaseFileModel
类用于文件,并在需要时实现UploadOnEloquentModel
并扩展该类。
<?php namespace App\Models; use DongttFd\LaravelUploadModel\Contracts\UploadOnEloquentModel; use DongttFd\LaravelUploadModel\Eloquent\UploadFileEloquent; use Illuminate\Database\Eloquent\Model; class BaseFileModel extends Model implements UploadOnEloquentModel { use UploadFileEloquent; }
除了可以从我的源代码扩展FileModel
外
<?php namespace App\Models; use DongttFd\LaravelUploadModel\Eloquent\FileModel; class User extends FileModel { }
覆盖属性
指定您将保存文件的驱动,默认为您的文件系统配置(config/filesystem.php
)
/** * Default save on disk (from keys of app/config/filesystem.php > disks) * * @var string */ protected $saveOnDisk = null;
指定您将保存文件的列,如果是JSON列,您必须使用array dot
头像列是文件
/** * Save file to avatar columns * * @var array */ protected $fileFields = ['avatar'];
card
列是对象
/** * The attributes that should be cast. * * @var array */ protected $casts = [ 'card' => 'array', ]; /** * Save files to photos columns * * @var array */ protected $fileFields = [ 'card.front', 'card.back' ];
photos
列是数组
/** * The attributes that should be cast. * * @var array */ protected $casts = [ 'photos' => 'array', ]; /** * Save files to photos columns * * @var array */ protected $fileFields = ['photos.*'];
photos
是variant
列中的数组
/** * The attributes that should be cast. * * @var array */ protected $casts = [ 'variant' => 'array', ]; /** * Save files to photos columns * * @var array */ protected $fileFields = ['variant.photos.*'];
指定您将保存文件到磁盘的文件夹
/** * Save path file to folder, format: ['<file-field>' => 'folder-name'] * * @var array */ protected $fileFolders = [ 'avatar' => 'avatar', 'card.front' => 'card-front', 'variant.photos.*' => 'variant-photos', ];
使用s3
驱动时,开启/关闭发布文件
/** * Only s3 amazon: save with publish file * * @var bool */ protected $filePublish = false;
使用s3
文件驱动时文件访问令牌的超时时间
/** * Only s3 amazon: expire time (minutes) off private file * * @var int */ protected $fileExpireIn = 5;
更新日志
请参阅更新日志以获取更多信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件我的邮箱报告,而不是使用问题跟踪器。
测试
composer test