esign/laravel-model-files

将文件与Laravel模型关联

1.3.0 2024-03-12 22:06 UTC

This package is auto-updated.

Last update: 2024-09-12 23:15:55 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

此包允许您以有见地的模式存储模型的文件。

安装

您可以通过composer安装此包

composer require esign/laravel-model-files

使用方法

准备模型

要将文件与模型关联,您需要在模型上使用Esign\ModelFiles\Concerns\HasFiles特性。

use Esign\ModelFiles\Concerns\HasFiles;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFiles;
}

数据库结构应如下所示

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->boolean('file')->default(0);
    $table->string('file_filename')->nullable();
    $table->string('file_mime')->nullable();
});

存储文件

要存储文件,您可以使用storeFile方法。此方法接受Illuminate\Http\FileIlluminate\Http\UploadedFile类的实例。

$post->storeFile('file', $request->file('attachment'));

检索文件信息

$post->hasFile('file'); // returns true/false
$post->getFolderPath('file'); // returns posts/file
$post->getFilePath('file'); // returns 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

删除文件

$post->deleteFile('file');

使用下划线可翻译

此包附带对underscore translatable包的支持。

请确保在translatable数组中包含文件、文件名和mime列

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',
    ];
}

接下来,您的迁移应如下所示

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();
});

您现在可以使用默认或特定区域设置的内联方法

$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

测试

composer test

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。