ssntpl/laravel-files

将文件与Eloquent模型关联

v0.1.1 2024-08-21 07:13 UTC

README

Latest Version on Packagist Total Downloads

这是一个简单的包,用于将文件与Laravel中的Eloquent模型关联。

安装

您可以通过Composer安装此包

composer require ssntpl/laravel-files

运行迁移

php artisan migrate

可选:您可以通过以下方式发布和运行迁移

php artisan vendor:publish --tag="laravel-files-migrations"
php artisan migrate

您可以通过以下方式发布配置文件

php artisan vendor:publish --tag="laravel-files-config"

这是已发布的配置文件的内容

return [
    /*
     * When using the "HasFiles" trait from this package, we need to know which
     * Eloquent model should be used to retrieve your files. Of course, it
     * is often just the "File" model but you may use whatever you like.
     *
     * The model you want to use as a File model needs to implement the
     * `Ssntpl\LaravelFiles\Contracts\File` contract.
     */

    'model' => Ssntpl\LaravelFiles\Models\File::class,

    /*
     * When using the "HasFiles" trait from this package, we need to know which
     * table should be used to retrieve your files. We have chosen a basic
     * default value but you may easily change it to any table you like.
     */

    'table' => 'files',

    /*
     * Define the hashing algorithm to calculate the checksum of the file content.
     * Most commonly used hashing algorithm are md5, sha1, sha256. For a complete
     * list of supported hashing algorithms, check hash_algos().
     */

    'checksum' => 'md5',
];

用法

HasFiles特性添加到您的模型中。

use Illuminate\Foundation\Auth\User as Authenticatable;
use Ssntpl\LaravelFiles\Traits\HasFiles;

class User extends Authenticatable
{
    use HasFiles;
}

向模型添加新文件。

$model = User::find(1);

$file = $model->createFile([
    
    // type: Optional. If missing, the deault type of the model is used.
    'type' => 'avatar', 

    // name: Optional. Represents the file name. If missing, a random uuid is generated.
    'name' => 'my_avatar.png',  

    // key: Optional. If missing the key is auto-generated from FilePrefix attribute and name of the file.
    'key' => 'avatar/user/1/my_avatar.png', 

    // disk: Optional. Represents the disk on which the file is stored. If missing the default disk is used.   
    'disk' => 's3', 

    // base64: Optional. If present the string is decoded and written to disk.
    'base64' => 'base64 encoded string of the content of the file.',

    // contents: Optional. base64 takes precedence over contents key. If both base64 and contents key are missing then you can add the contents to the file later.
    'contents' => 'you can directly provide the contents instead of the base64 string',
]);

定义模型的FilePrefix。

class Flight extends Model 
{
    HasFiles;

    Protected $file_prefix = 'flights';
}

动态定义FilePrefix。有时我们不需要固定的路径来存储模型的全部文件。您可以使用FilePrefix属性创建动态文件前缀。

class Flight extends Model 
{
    HasFiles;

    public function getFilePrefixAttribute()
    {
        return 'flights/' . $this->id;       
    }
}

访问文件模型。

$file->url; // Returns the url() of the file.

$file->exists(); // Boolean. Checks if the file exists on the disk.

echo $file; // Prints the url of the file.

echo $file->base64; // Prints the base64 encoded string of the file contents.

$file->base64 = 'base64 contents'; // Writes the content to the disk.

echo $file->contents; // Print the file contents.

$file->contents = 'file contents'; // Writes the contents to the disk.

获取单个文件。如果没有文件或文件多于一个,将抛出异常。

$model->file;           // Fetches single file of default type. 

$model->file();         // Fetches single file of default type.

$model->file($type);    // Fetches single file of $type type.

获取多个文件关联。

$files = $model->files();   // Fetches relation to all the files belonging to the model.

$files->get(); // Fetches all the files of any type that belong to the model.

$files = $model->files($type);   // Fetches relation to all the files of type $type that belong to the model.

$files->get(); // Fetches all the files of type $type.

测试

composer test

待办事项

  • 声明Ssntpl\LaravelFiles\Contracts\File契约
  • 为每个模型添加定义默认磁盘选项
  • 添加返回文件完整路径的path()方法
  • 使文件模型成为Illuminate\Http\File的子类,并检查所有方法是否正常工作。
  • 检查是否可以在特性中修改destroy/delete方法以处理文件对象。

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

安全漏洞

请查看我们的安全策略了解如何报告安全漏洞。

致谢

许可证

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