esign/laravel-model-files
Composer 安装命令:
composer require esign/laravel-model-files
包简介
Associate files with your Laravel Models
关键字:
README 文档
README
This package allows you to store files for models in an opinionated way.
Installation
You can install the package via composer:
composer require esign/laravel-model-files
Usage
Preparing your model
To associate files with your model you need to use the Esign\ModelFiles\Concerns\HasFiles trait on the model.
use Esign\ModelFiles\Concerns\HasFiles; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFiles; }
The database structure should look like the following:
Schema::create('posts', function (Blueprint $table) { $table->id(); $table->boolean('file')->default(0); $table->string('file_filename')->nullable(); $table->string('file_mime')->nullable(); });
Configuring the disk
By default, the files will be associated with the default disk configured in your config/filesystems.php file.
You may override this by defining the getFileDisk method on your model.
use Esign\ModelFiles\Concerns\HasFiles; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFiles; public function getFileDisk(): string { return 'public'; } }
Storing files
To store files you may use the storeFile method.
This method accepts instances of both the Illuminate\Http\File and Illuminate\Http\UploadedFile classes.
$post->storeFile('file', $request->file('attachment'));
To store raw string content, use the storeFileFromString method.
$post->storeFileFromString('file', 'Hello world'); $post->storeFileFromString('file', '{"key":"value"}');
Retrieving file info
$post->hasFile('file'); // returns true/false $post->getFolderPath('file'); // returns posts/file $post->getFilePath('file'); // returns posts/file/1.pdf $post->getFilePathOnDisk('file'); // returns /path/to/storage/app/public/posts/file/1.pdf $post->getFileMime('file'); // returns application/pdf $post->getFileExtension('file'); // returns pdf $post->getFileUrl('file'); // returns https://www.example.com/storage/posts/file/1.pdf $post->getVersionedFileUrl('file'); // returns https://www.example.com/storage/posts/file/1.pdf?t=1675776047
Deleting files
$post->deleteFile('file');
Using with underscore translatable
This package ships with support for the underscore translatable package.
Make sure to include the file, filename and mime columns within the translatable array:
use Esign\ModelFiles\Concerns\HasFiles; use Esign\UnderscoreTranslatable\UnderscoreTranslatable; use Illuminate\Database\Eloquent\Model; class UnderscoreTranslatablePost extends Model { use HasFiles; use UnderscoreTranslatable; public $translatable = [ 'document', 'document_filename', 'document_mime', ]; }
Next up, your migrations should look like the following:
Schema::create('posts', function (Blueprint $table) { $table->id(); $table->boolean('document_en')->default(0); $table->boolean('document_nl')->default(0); $table->string('document_filename_en')->nullable(); $table->string('document_filename_nl')->nullable(); $table->string('document_mime_en')->nullable(); $table->string('document_mime_nl')->nullable(); });
You may now use the internal methods using the default or the specific locale:
$post->hasFile('document'); // returns true/false $post->getFolderPath('document'); // returns posts/document_en $post->getFilePath('document'); // returns posts/document_en/1.pdf $post->getFileMime('document'); // returns application/pdf $post->getFileExtension('document'); // returns pdf $post->getFileUrl('document'); // returns https://www.example.com/storage/posts/document_en/1.pdf $post->getVersionedFileUrl('document'); // returns https://www.example.com/storage/posts/document_en/1.pdf?t=1675776047
$post->hasFile('document_en'); // returns true/false $post->getFolderPath('document_en'); // returns posts/document_en $post->getFilePath('document_en'); // returns posts/document_en/1.pdf $post->getFileMime('document_en'); // returns application/pdf $post->getFileExtension('document_en'); // returns pdf $post->getFileUrl('document_en'); // returns https://www.example.com/storage/posts/document_en/1.pdf $post->getVersionedFileUrl('document_en'); // returns https://www.example.com/storage/posts/document_en/1.pdf?t=1675776047
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.
esign/laravel-model-files 适用场景与选型建议
esign/laravel-model-files 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.49k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「esign」 「model-files」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 esign/laravel-model-files 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 esign/laravel-model-files 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 esign/laravel-model-files 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A laravel package to make your eloquent models translatable.
Manage SEO tags within your Laravel application
Load translations from the database or other sources.
Generate slugs when saving UnderscoreTranslatable models
Throttle exceptions being sent to Sentry
A Laravel SDK for the Plytix API
统计信息
- 总下载量: 12.49k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-07