lupennat/nova-expandable-many
最新稳定版本:v3.3.0
Composer 安装命令:
composer require lupennat/nova-expandable-many
包简介
Laravel Nova - Expandable Many
README 文档
README
Requirements
php: ^7.4 | ^8laravel/nova: ^4
Installation
composer require lupennat/nova-expandable-many:^3.0
| NOVA | PACKAGE |
|---|---|
| <4.29.5 | 1.x |
| <4.32.6 | 2.x |
| >=4.32.6 | 3.x |
Usage
Register Trait HasExpandableMany globally on Resources.
the Trait include an override of
indexFieldsmethod, it call methodwithoutListableFieldsNotExpandableinstead ofwithoutListableFieldson FieldCollection; if you already register a custom ovveride ofindexFieldsyou can do it manually without using provided trait.
ExpandableMany Package automatically enable a new method expandable for all Many Relationship Fields:
- BelongsToMany
- HasMany
- HasManyThrough
- MorphedByMany
- MorphMany
- MorphToMany
The relation table (without any custom actions and lenses) will be displayed as a collapsable row on the index page.
use Laravel\Nova\Fields\HasMany; use Laravel\Nova\Http\Requests\NovaRequest; class User extends Resource { use Lupennat\ExpandableMany\HasExpandableMany; public function fields(Request $request) { return [ HasMany::make('User Post', 'posts', Post::class)->expandable(); ]; } }
By Default expandable use Show and Hide as labels, you can change labels using meta
HasMany::make('User Post', 'posts', Post::class)->expandable() ->withMeta([ 'expandableShowLabel' => 'Custom Show', 'expandableHideLabel' => 'Custom Hide', ])
you can also define a custom HTML using
HasMany::make('User Post', 'posts', Post::class)->expandable() ->withMeta([ 'expandableShowHtml' => '<strong>Custom Show Html</strong>', 'expandableHideHtml' => '<strong>Custom Hide Html</strong>', ])
if both custom label and HTML are defined, HTML will be used
By Default expandable do not store on browser history any status, you can change it using meta
HasMany::make('User Post', 'posts', Post::class)->expandable() ->withMeta([ // 'expandableStoreStatus' => 'full', // will store status also for relationships 'expandableStoreStatus' => 'accordion', // will store status only for accordion 'expandableStoreStatus' => '', // will not store any status ])
Expandable can be skipped and an empty field is shown (by Default is false)
HasMany::make('User Post', 'posts', Post::class)->expandable() ->withMeta([ 'expandableSkip' => true, 'expandableSkipLabel' => '', // default is '—' ])
Expandable can disable standard actions (By Default is enabled)
HasMany::make('User Post', 'posts', Post::class)->expandable() ->withMeta([ 'expandableUseStandardActions' => false, // disable create/edit/view/delete/restore ])
Display Callback
Expandable Many will resolve a display callback foreach resource, you can use it to manipulate meta attributes dinamically.
HasMany::make('User Post', 'posts', Post::class) ->expandable(function(HasMany $field, $resource) { $resource->loadCount('posts'); $field->withMeta([ 'expandableShowLabel' => 'Show ' . $resource->posts_count, 'expandableSkip' => $resource->posts_count === 0 ]); })
By Default Expandable do not resolve relations, accessing a relationAttribute through the
$resourcewill execute a query against database to load all related models.
Lens
If you want to use Expandable Many inside a Lens, you need to register also the trait HasExpandableManyLens inside your Lens.
use Laravel\Nova\Fields\HasMany; use Laravel\Nova\Http\Requests\NovaRequest; use Laravel\Nova\Lenses\Lens; class UserLens extends Lens { use Lupennat\ExpandableMany\HasExpandableManyLens; public function fields(Request $request) { return [ HasMany::make('User Post', 'posts', Post::class)->expandable(); ]; } }
Credits
This package is based on the original idea from Nova Expandable Row
统计信息
- 总下载量: 37.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-05
