cyneek / laravel-multiple-stapler
Laravel Stapler的多文件上传。
Requires
- php: >=5.4.0
- codesleeve/laravel-stapler: 1.0.*
- laravel/framework: 4.*|5.*
This package is not auto-updated.
Last update: 2024-09-20 22:23:04 UTC
README
西班牙语README
索引
变更
v0.1.1
将hasAttachedFile
方法改为hasOneAttachedFile
以提高与CodeSleeve/laravel-stapler
包的兼容性。现在您可以在模型中同时使用这两个包。
安装
使用以下命令通过composer添加此包
composer require cyneek/laravel-multiple-stapler
更新composer后,将服务提供者添加到config/app.php
中的providers
数组。
Codesleeve\LaravelStapler\Providers\L5ServiceProvider::class, Cyneek\LaravelMultipleStapler\Providers\LaravelMultipleStaplerProvider::class
从命令行使用迁移工具;它将创建一个基本表,用于处理与应用程序模型关联的所有文件数据。
php artisan migrate
Aaaaand it's done!
描述
方法
-
hasOneAttachedFile:用于将一个参数与一个文件链接的方法。
-
hasMultipleAttachedFiles:允许将多个文件以图库样式链接到一个参数的方法。
这两个方法都接受以下参数
-
name:[String|Required] 将分配给模型中文件参数的名称。
-
options:[Array|Required] 用于处理链接文件的Stapler选项。如需了解此库中可用的选项,请点击此处查看Stapler官方文档。
-
attachedModelClass:[String|Optional] 您可以在此处定义一个模型类名,用于处理文件信息而不是默认的
StaplerFiles
。模型必须实现LaravelStaplerInterface
接口。
Stapler属性的配置
声明单个文件属性
这是Laravel Stapler库中的常规行为。它允许上传单个文件并将其链接到已加载的模型属性,允许进行所有在纯Stapler文件附件中可进行的操作。
要创建一个链接到模型中单个文件的属性,您需要按照以下步骤操作
- 将
MultipleFileTrait
特性添加到模型中
class Example extends \Eloquent { use MultipleFileTrait;
- 在
__construct()
方法中使用hasOneAttachedFile
方法添加您想要添加的属性。
function __construct(array $attributes = [] ) { $this->hasOneAttachedFile('avatar', [ 'styles' => [ 'medium' => '300x300', 'thumb' => '100x100' ] ]); parent::__construct($attributes); }
警告:必须在调用父构造函数之前添加参数创建方法。
声明多个文件属性
这得益于Laravel的多态表,它们将存储所有文件数据并将其与父模型对象关联,这归功于字段fileable_id
、fileable_type
和fileable_field
,这些字段将存储参数名称。
要创建一个模型中的多个文件处理属性,您需要按照以下步骤操作
- 将
MultipleFileTrait
特性添加到模型中
class Example extends \Eloquent { use MultipleFileTrait;
- 在
__construct()
方法中使用hasMultipleAttachedFiles
方法添加您想要添加的属性。
function __construct(array $attributes = [] ) { $this->hasMultipleAttachedFiles('images', [ 'styles' => [ 'medium' => '300x300', 'thumb' => '100x100' ] ]); parent::__construct($attributes); }
警告:必须在调用父构造函数之前添加参数创建方法。
文件插入
在单个文件属性中插入文件
对于此示例,我们将使用一个表单,该表单将上传单个文件到我们的模型属性。也可以使用接受多个文件的表单字段,但在这些情况下,除第一个上传的文件外,所有其他文件将被自动丢弃。
在进行表单的 update
操作时,请注意,如果该属性中链接了之前的文件,上传新文件时将自动删除。
表单视图
<?= Form::open(['url' => action('ExampleController@store'), 'method' => 'POST', 'files' => true]) ?> <?= Form::input('name') ?> <?= Form::input('description') ?> <?= Form::file('avatar') ?> <?= Form::submit('save') ?> <?= Form::close() ?>
控制器处理程序
public function store() { // Create and save a new Example object, mass assigning all of the input fields (including the 'avatar' file field). $example = Example::create(Input::all()); }
这是上传文件并将其链接到新对象 Model 所需的最少代码。有关访问文件数据的说明,请参阅上面的 Readme。
在多个文件属性中插入文件
需要有一个具有文件参数的 Model,该参数能够通过 hasMultipleAttachedFiles
方法处理多个文件。
表单视图
<?= Form::open(['url' => action('ExampleController@storeMultiple'), 'method' => 'POST', 'files' => true]) ?> <?= Form::input('name') ?> <?= Form::input('description') ?> <!-- Note that the field name now it's written as an array and the additional option in the file() method --> <?= Form::file('avatar[]', ['multiple' => true]) ?> <?= Form::submit('save') ?> <?= Form::close() ?>
控制器处理程序
public function storeMultiple() { // Create and save a new Example object, mass assigning all of the input fields (including the 'avatar' file field). $example = Example::create(Input::all()); }
如您所见,在处理多个或单个文件时,唯一的区别在于 Model 定义和表单。系统将处理每个上传文件的存储,并将其链接到加载的 Model。
访问上传的文件数据
访问单个文件参数
要从加载的对象访问链接的文件数据,只需调用其参数名称,就像它是一个正常的 Laravel 关系一样。
$example = Example::find(1); $example->avatar->createdAt();
访问多个文件参数
与上一个示例不同的是,它将返回一个包含所有链接文件的 Collection,而不是返回一个附加对象,因此如果您想与之交互,必须像访问数组一样遍历每个文件对象。
$example = Example::find(1); foreach ($example->avatar as $avatar) { echo $avatar->file->createdAt(); }
删除关联文件
您必须始终记住,如果您删除带有附加链接文件的父对象,这些文件也会自动删除,因此
Example::delete(1);
将删除 ID 为 1 的 Example 对象及其所有链接文件。
显式删除单个文件属性
这与 Laravel 的多态关系具有相同的工作方式。
$example->avatar()->delete();
显式删除多个文件属性
在这种情况下,您需要遍历每个文件对象以删除它。
foreach ($example->avatar as $avatar) { echo $avatar->delete(); }
已知问题
已知 Laravel 5.* 存在一个问题,在使用已安装 codesleeve/laravel-stapler
包的 php artisan
时会抛出异常。
[ErrorException]
Missing argument 2 for Codesleeve\LaravelStapler\Providers\ServiceProvider::Codesleeve\LaravelStapler\Providers\{closure}(), called in */Laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 678 and defined