ardenthq/nova-image-gallery-field

为Nova提供的一个字段,允许您上传多张图片并对它们进行排序

2.2.0 2023-08-18 16:02 UTC

This package is auto-updated.

Last update: 2024-09-09 23:45:34 UTC


README

一个自定义的Nova字段,允许多张图片上传并排序

Stable Version License PHP Version Require

功能

  • 适用于 Laravel Nova ^4.0
  • 将多张图片上传到 spatie/laravel-medialibrary 集合。
  • 图片排序
  • 自定义图片验证
  • 拖放
  • 暗色模式

安装

composer require ardenthq/nova-image-gallery-field

要求

使用

注意 此包重用了在 Laravel\Nova\Fields\Trix 字段上用于存储和处理文件的逻辑。这意味着我们需要遵循 Laravel Nova 文档 中提到的相关步骤,添加迁移和修剪文件。

  1. 定义两个数据库表来存储挂起和持久化的Trix上传。为此,创建一个迁移,包含以下表定义
Schema::create('nova_pending_field_attachments', function (Blueprint $table) {
    $table->increments('id');
    $table->string('draft_id')->index();
    $table->string('attachment');
    $table->string('original_name');
    $table->string('disk');
    $table->timestamps();
});

Schema::create('nova_field_attachments', function (Blueprint $table) {
    $table->increments('id');
    $table->string('attachable_type');
    $table->unsignedInteger('attachable_id');
    $table->string('attachment');
    $table->string('disk');
    $table->string('url')->index();
    $table->timestamps();

    $table->index(['attachable_type', 'attachable_id']);
});

注意:最新的nova已包含这些迁移。如果已包含,则在运行迁移时可能会收到错误 Table already exists。在这种情况下,您可能只需要在 nova_pending_field_attachments 中添加 original_name 字段。

Schema::table('nova_pending_field_attachments', function (Blueprint $table) {
    $table->string('original_name')->after('attachment');
});
  1. 在您的 app/Console/Kernel.php 文件中,您应该注册一个每日作业,以修剪挂起附件表和存储中的任何过时附件。Laravel Nova提供了完成此操作的作业实现。
use Laravel\Nova\Trix\PruneStaleAttachments;

$schedule->call(function () {
    (new PruneStaleAttachments)();
})->daily();
  1. ImageGalleryField 字段添加到您的 Nova 资源中。

  2. 考虑到图片将根据通过字段 make 方法传递的第一个参数或第二个参数(如果设置)的名称存储在 Spatie\MediaLibrary\MediaCollections\Models\Media 集合中。(不要忘记 在模型中注册媒体集合

  3. 使用 rules 方法定义每个图片使用的规则。

  4. 使用 rulesMessages 方法为图片规则定义自定义验证消息。

  5. 如果您想在拖放区域内放置“帮助”文本,请使用 help 方法。

示例

<?php
namespace App\Nova;

use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Resource;
use Ardenthq\ImageGalleryField\ImageGalleryField;

final class ResourceName extends Resource
{
    // ....
    public function fields(NovaRequest $request)
    {
        return [
            // ....
            ImageGalleryField::make('Images')
                ->rules('mimes:jpeg,png,jpg,gif', 'dimensions:min_width=150,min_height=150', 'max:5000')
                ->rulesMessages([
                    'mimes'      => 'You must use a valid jpeg, png, jpg or gif image.',
                    'max'        => 'The image must be less than 5MB.',
                    'dimensions' => 'The image must be at least 150px wide and 150px tall.',
                ])
                ->help('Min size 150 x 150. Max filesize 5MB.')
                // Optional: add this method if you want to show the first image
                // of the gallery on the index page
                ->showOnIndex(),
                // ...
        ];
    }
    // ...
}

兼容分支

由于Nova 4.22中对Trix字段的更改,此包在 main 分支上不支持低于Nova v4的版本。如果您想在较旧的Nova版本上使用此包,请查看此存储库中的 nova-compatibility 分支,并使用它通过composer安装包:composer require ardenthq/nova-image-gallery-field:dev-nova-compatibility

开发

  1. 运行 yarn nova:installyarn install 来安装编译视图组件所需的所有必要依赖项。
  2. 在本地环境中对组件进行更改时,运行 yarn run dev(或 yarn run watch)。
  3. 如果更改了vue组件,确保在提交PR之前进行生产编译。

编译为生产

  1. 运行 yarn nova:installyarn install 来安装编译视图组件所需的所有必要依赖项。
  2. 运行 yarn run production

使用 phpstan 分析代码

composer analyse

使用php rector 重构代码

composer refactor

使用 php-cs-fixer 格式化代码

composer format

运行测试

composer test

安全

如果您在此软件包中发现安全漏洞,请发送电子邮件至 security@ardenthq.com。我们将及时处理所有安全漏洞。

致谢

本项目的存在离不开所有 贡献者

许可证

MIT © Ardent