dmitrybubyakin/nova-medialibrary-field 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dmitrybubyakin/nova-medialibrary-field

Composer 安装命令:

composer require dmitrybubyakin/nova-medialibrary-field

包简介

Laravel Nova field for managing the Spatie media library.

README 文档

README

Latest Version on Packagist Total Downloads

Laravel Nova field for managing the Spatie media library.

This is the documentation for v2 and v3. For v1 follow this link

Features:

  • add media on update/create views
  • add existing media
  • crop media
  • sort media
  • display on the index view

Table of Contents

Screenshots

index view create view details view update view media actions media crop dialog media details dialog existing media dialog

Installation

This package can be installed via command:

composer require dmitrybubyakin/nova-medialibrary-field

Usage

Medialibrary::make($name, $collectionName = '', $diskName = '', $attribute = null),

Methods

Attribute

Sometimes you may need to use the same field label (duplicated sections, etc). The attribute must be unique. In this case you can change the default behaviour using the attribute() method.

Medialibrary::make('name', 'collection name', 'disk name', 'custom_attribute');
// or
Medialibrary::make('name', 'collection name', 'disk name')->attribute('custom_attribute');

Fields

Define custom fields for media. MediaFields is used by default.

Medialibrary::make('Media')->fields(function () {
    return [
        Text::make('File Name', 'file_name')
            ->rules('required', 'min:2'),

        Text::make('Tooltip', 'custom_properties->tooltip')
            ->rules('required', 'min:2'),

        GeneratedConversions::make('Conversions')
            ->withTooltips(),
    ];
});

ResolveMediaUsing

Medialibrary::make('Media')->resolveMediaUsing(function (HasMedia $model, string $collectionName) {
    return $model->getMedia($collectionName);
});

AttachUsing

Called inside AttachController. AttachCallback is used by default. It accepts $fieldUuid which is used when a resource is not created. If you want to attach media on the create view, you should keep these lines in your callback.

Medialibrary::make('Media')
    ->attachUsing(function (HasMedia $model, UploadedFile $file, string $collectionName, string $diskName, string $fieldUuid) {
        if ($model instanceof TransientModel) {
            $collectionName = $fieldUuid;
        }

        $fileAdder = $model->addMedia($file);

        // do something

        $fileAdder->toMediaCollection($collectionName, $diskName);
    });

AttachExisting

Allow attaching existing media.

Medialibrary::make('Media')->attachExisting(); // display all media
Medialibrary::make('Media')->attachExisting('collectionName'); // display media from a specific collection
Medialibrary::make('Media')->attachExisting(function (Builder $query, Request $request, HasMedia $model) {
    $query->where(...);
});

MediaOnIndex

Display media on index

Medialibrary::make('Media')->mediaOnIndex(1);
Medialibrary::make('Media')->mediaOnIndex(function (HasMedia $resource, string $collectionName) {
    return $resource->media()->where('collection_name', $collectionName)->limit(5)->get();
});

DownloadUsing

Medialibrary::make('Media')->downloadUsing('conversionName');
Medialibrary::make('Media')->downloadUsing(function (Media $media) {
    return $media->getFullUrl();
});

PreviewUsing

Medialibrary::make('Media')->previewUsing('conversionName');
Medialibrary::make('Media')->previewUsing(function (Media $media) {
    return $media->getFullUrl('preview');
});

Tooltip

Medialibrary::make('Media')->tooltip('file_name');
Medialibrary::make('Media')->tooltip(function (Media $media) {
    return $media->getCustomProperty('tooltip');
});

Title

Medialibrary::make('Media')->title('name');
Medialibrary::make('Media')->title(function (Media $media) {
    return $media->name;
});

CopyAs

Medialibrary::make('Media')->copyAs('Url', function (Media $media) {
    return $media->getFullUrl();
});

Medialibrary::make('Media')->copyAs('Html', function (Media $media) {
    return $media->img();
}, 'custom-icon');

// You can hide default "Copy Url"
Medialibrary::make('Media')->hideCopyUrlAction();

Croppable

https://github.com/fengyuanchen/cropperjs#options

Medialibrary::make('Media')->croppable('conversionName');
Medialibrary::make('Media')->croppable('conversionName', ['viewMode' => 3]);
Medialibrary::make('Media')->croppable('conversionName', [
    'rotatable' => false,
    'zoomable' => false,
    'cropBoxResizable' => false,
]);
Medialibrary::make('Media')->croppable('conversionName', function (Media $media) {
    return $media->getCustomProperty('croppable') ? ['viewMode' => 3] : null;
});

https://docs.spatie.be/laravel-medialibrary/v8/converting-images/defining-conversions/#performing-conversions-on-specific-collections

{note} If your media in different collection, make sure pass your collectionName to performOnCollections

$this->addMediaConversion('conversionName')->performOnCollections('collectionName')

Single

https://docs.spatie.be/laravel-medialibrary/v7/working-with-media-collections/defining-media-collections/#single-file-collections

Medialibrary::make('Media')->single();

Accept

Medialibrary::make('Media')->accept('image/*');

MaxSizeInBytes

Medialibrary::make('Media')->maxSizeInBytes(1024 * 1024);

AttachOnDetails

Allows attaching files on the details view.

Medialibrary::make('Media')->attachOnDetails();

AttachRules

Medialibrary::make('Media')->attachRules('image', 'dimensions:min_width=500,min_height=500');

Autouploading

Medialibrary::make('Media')->autouploading();

Preview Customization

Medialibrary::make('Media')->withMeta([
    'indexPreviewClassList' => 'rounded w-8 h-8 ml-2',
    'detailsPreviewClassList' => 'w-32 h-24 rounded-b',
]);

Validation

Medialibrary::make('Media')
    ->rules('array', 'required') // applied to the media collection
    ->creationRules('min:2') // applied to the media collection
    ->updateRules('max:4') // applied to the media collection
    ->attachRules('image', 'dimensions:min_width=500,min_height=500'); // applied to media

Sorting

Medialibrary::make('Media')->sortable();

Authorization Gates 'view', 'update' and 'delete'

To view, update and delete uploaded media, you need to setup some gates. You can use the store and replace callbacks to store additional information to the custom_properties. The additional information can be used inside the gates for authorization.

Gate::define('view', function ($user, $media) {
    return true; // view granted
});

Gate::define('update', function ($user, $media) {
    return true; // update granted
});

Gate::define('delete', function ($user, $media) {
    return true; // deletion granted
});

You can also use the policy.

class MediaPolicy
{
    public function view(User $user, Media $media): bool
    {
        return true;
    }

    public function update(User $user, Media $media): bool
    {
        return true;
    }

    public function delete(User $user, Media $media): bool
    {
        return true;
    }
}

class AuthServiceProvider extends ServiceProvider
{
    protected $policies = [
        Media::class => MediaPolicy::class,
    ];

    //...
}

Translations

Changelog

Please see the CHANGELOG for more information about the most recent changed.

Alternatives

License

The MIT License (MIT). Please see License File for more information.

dmitrybubyakin/nova-medialibrary-field 适用场景与选型建议

dmitrybubyakin/nova-medialibrary-field 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 652.03k 次下载、GitHub Stars 达 268, 最近一次更新时间为 2018 年 12 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「nova」 「laravel」 「medialibrary」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dmitrybubyakin/nova-medialibrary-field 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dmitrybubyakin/nova-medialibrary-field 我们能提供哪些服务?
定制开发 / 二次开发

基于 dmitrybubyakin/nova-medialibrary-field 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 652.03k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 269
  • 点击次数: 32
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 268
  • Watchers: 4
  • Forks: 61
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-12-22