achrafbardan/ nova-medialibrary-field
Laravel Nova 字段,用于管理 Spatie 媒体库。
3.2.14
2022-02-12 21:36 UTC
Requires
- php: ^7.4|^8.0|^8.1
- laravel/nova: ^3.0
- spatie/laravel-medialibrary: ^8.0|^9.0|^10.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: ^5.0|^6.0|^7.0
- phpunit/phpunit: ^8.5|^9.0
- dev-master
- 3.2.14
- 3.2.13
- 3.2.12
- 3.2.11
- 3.2.10
- 3.2.9
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.1
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- v2.x-dev
- 2.1.2
- 2.1.1
- 2.0.20
- 2.0.19
- 2.0.18
- 2.0.17
- 2.0.16
- 2.0.15
- 2.0.14
- 2.0.13
- 2.0.12
- 2.0.11
- 2.0.10
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- dev-upgrade-version
- dev-dependabot/npm_and_yarn/follow-redirects-1.14.7
This package is auto-updated.
Last update: 2024-09-11 14:57:24 UTC
README
Laravel Nova 字段,用于管理 Spatie 媒体库。
这是 v2 和 v3 版本的文档。对于 v1 版本,请点击此链接
特性
- 在更新/创建视图中添加媒体
- 添加现有媒体
- 裁剪媒体
- 排序媒体
- 在索引视图中显示
目录
屏幕截图
安装
可以通过以下命令安装此包
composer require dmitrybubyakin/nova-medialibrary-field
用法
Medialibrary::make($name, $collectionName = '', $diskName = '', $attribute = null),
方法
属性
有时您可能需要使用相同的字段标签(重复部分等)。属性必须是唯一的。在这种情况下,您可以使用 attribute()
方法更改默认行为。
Medialibrary::make('name', 'collection name', 'disk name', 'custom_attribute'); // or Medialibrary::make('name', 'collection name', 'disk name')->attribute('custom_attribute');
字段
为媒体定义自定义字段。默认使用 MediaFields。
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
在 AttachController 内部调用。默认使用 AttachCallback。它接受 $fieldUuid
,当资源未创建时使用。如果您想在创建视图中附加媒体,应在您的回调中保留 这些行。
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
允许附加现有媒体
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
在索引中显示媒体
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'); });
工具提示
Medialibrary::make('Media')->tooltip('file_name'); Medialibrary::make('Media')->tooltip(function (Media $media) { return $media->getCustomProperty('tooltip'); });
标题
Medialibrary::make('Media')->title('name'); Medialibrary::make('Media')->title(function (Media $media) { return $media->name; });
复制为
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();
可裁剪
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; });
{note} 如果您的媒体在不同的集合中,请确保将您的 collectionName 传递给
performOnCollections
$this->addMediaConversion('conversionName')->performOnCollections('collectionName')
单个
Medialibrary::make('Media')->single();
接受
Medialibrary::make('Media')->accept('image/*');
最大尺寸(字节)
Medialibrary::make('Media')->maxSizeInBytes(1024 * 1024);
在详情视图中附加
允许在详情视图中附加文件。
Medialibrary::make('Media')->attachOnDetails();
附加规则
Medialibrary::make('Media')->attachRules('image', 'dimensions:min_width=500,min_height=500');
自动上传
Medialibrary::make('Media')->autouploading();
预览定制
Medialibrary::make('Media')->withMeta([ 'indexPreviewClassList' => 'rounded w-8 h-8 ml-2', 'detailsPreviewClassList' => 'w-32 h-24 rounded-b', ]);
验证
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
排序
Medialibrary::make('Media')->sortable();
授权门 '查看'、'更新' 和 '删除'
要查看、更新和删除上传的媒体,您需要设置一些权限门。您可以使用 store 和 replace 回调将其他信息存储到 custom_properties 中。这些附加信息可用于权限门中进行授权。
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 });
您还可以使用策略。
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, ]; //... }
翻译
变更日志
有关最近更改的更多信息,请参阅变更日志
替代方案
许可
MIT 许可证 (MIT)。有关更多信息,请参阅许可文件