armincms / many-to-many
一个 Laravel Nova 字段。
1.2.1
2023-02-20 16:52 UTC
Requires
- php: >=7.1.0
- laravel/nova: ^4.0
- dev-master
- 1.2.1
- 1.2.0
- 1.1.1
- v1.1.0
- v1.0.2
- v1.0.1
- v1.0.0
- 0.4.0
- 0.3.0
- 0.2.1
- 0.2.0
- 0.1.0
- dev-dependabot/npm_and_yarn/loader-utils-1.4.2
- dev-dependabot/npm_and_yarn/decode-uri-component-0.2.2
- dev-dependabot/npm_and_yarn/json5-1.0.2
- dev-dependabot/npm_and_yarn/webpack-5.76.1
- dev-dependabot/npm_and_yarn/minimist-1.2.8
- dev-dependabot/npm_and_yarn/dns-packet-5.4.0
This package is auto-updated.
Last update: 2024-09-08 04:24:04 UTC
README
用于多态和非多态 ManyToMany
关系的 Laravel Nova 字段。
目录
特性
- 在创建和更新页面中附加多态和非多态
ManyToMany
关系 - 在附加关系时编辑交叉列
- 多次将一个资源附加到另一个资源
安装
composer require armincms/many-to-many
简单用法
use Armincms\Fields\BelongsToMany;
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
BelongsToMany::make(__("Label"), 'relationName', RelatedResource::class)
->fields(function() {
return [
Text::make('Price')
->rules('required', 'numeric'),
];
})
->pivots(),
];
}
搜索
要搜索关系值而不是选择它;您可以在字段上使用 searchable
方法。
交叉表
要自定义附加资源时的交叉列,您可以使用字段上的 pivots
方法。然后使用 fields
方法定义您自己的自定义交叉字段。现在,在附加资源时;一个包含交叉字段的模态框将显示给您。
除了交叉列外,交叉表还应该有自己的 id
列,该列必须在模型定义中提及。
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Role extends Model { /** * The users that belong to the role. */ public function users() { return $this->belongsToMany(User::class)->withPivot('id', 'active', 'created_by'); } }
use Armincms\Fields\BelongsToMany;
/**
* Get the fields displayed by the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function fields(NovaRequest $request)
{
return [
BelongsToMany::make(__("Label"), 'relationName', RelatedResource::class)
->fields(function() {
return [
Text::make('Price')
->rules('required', 'numeric'),
];
})
->pivots(),
];
}
重复附件
您可以使用 duplicate
功能重复地将一个资源附加到另一个资源。请参考以下示例
use Armincms\Fields\BelongsToMany;
/**
* Get the fields displayed by the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function fields(NovaRequest $request)
{
return [
BelongsToMany::make(__("Label"), 'relationName', RelatedResource::class)
->fields(function() {
return [
Text::make('Price')
->rules('required', 'numeric'),
];
})
->duplicate(),
];
}
多态关系
对于多态关系的使用与非多态关系相同。请参考以下示例
use Armincms\Fields\MorphToMany;
/**
* Get the fields displayed by the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function fields(NovaRequest $request)
{
return [
MorphToMany::make(__("Label"), 'relationName', RelatedResource::class)
->fields(function() {
return [
Text::make('Price')
->rules('required', 'numeric'),
];
})
->duplicate()
->pivots(),
];
}
或
use Armincms\Fields\MorphedByMany;
/**
* Get the fields displayed by the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function fields(NovaRequest $request)
{
return [
MorphedByMany::make(__("Label"), 'relationName', RelatedResource::class)
->fields(function() {
return [
Text::make('Price')
->rules('required', 'numeric'),
];
})
->duplicate()
->pivots(),
];
}
使用填充
您可以使用 fillUsing
来更改交叉列的值;然后您需要返回一个与您的交叉表匹配的关联数组。请注意;“fillUsing”方法适用于每个附件。请参阅以下示例
->fillUsing(function($pivots) {
if(isset($pivots['options']) && is_array($pivots['options'])) {
$pivots['options'] = json_encode($pivots['options']);
}
return $pivots;
}),
过滤相关资源
您可以使用 withAttachableFilters
添加过滤器,根据相关资源的属性获取相关资源选项。请参阅以下示例
use Armincms\Fields\MorphToMany;
/**
* Get the fields displayed by the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function fields(NovaRequest $request)
{
return [
MorphToMany::make(__("Label"), 'relationName', RelatedResource::class)
->withAttachableFilters(['attribute1' => 'value1', 'attribute2' => 'value2']),
];
}