dynamicnet/many-to-many
最新稳定版本:1.0.1
Composer 安装命令:
composer require dynamicnet/many-to-many
包简介
A Laravel Nova field.
README 文档
README
A Laravel Nova field for polymorphic and non-polymorphic ManyToMany relationships.
Table of Contents
Features
- Attach polymorphic and non-polymorphic
ManyToManyrelationships in the creation and update page - Edit pivot columns when attaching relation
- Attach a source to another resource many times
Install
composer require martinjuul/many-to-many
Simple Usage
use Juul\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(),
];
}
Pivots
For customizing the pivot columns when attaching a resource you can use the pivots method of the field. then define your custom pivot fields with the fields method. now, when attaching a resource; a Modal that contains the pivot fields will be displayed to you.
use Juul\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(),
];
}
Duplicate Attachment
You can use the duplicate feature for repetitively attach a resource to another resource. follow the example:
use Juul\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'),
];
})
->duplicate(),
];
}
Polymorphic Relation
Using for the polymorphic relationships is like non-polymorphic. follow the example:
use Juul\Fields\MorphToMany;
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
MorphToMany::make(__("Label"), 'relationName', RelatedResource::class)
->fields(function() {
return [
Text::make('Price')
->rules('required', 'numeric'),
];
})
->duplicate()
->pivots(),
];
}
or
use Juul\Fields\MorphedByMany;
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
MorphedByMany::make(__("Label"), 'relationName', RelatedResource::class)
->fields(function() {
return [
Text::make('Price')
->rules('required', 'numeric'),
];
})
->duplicate()
->pivots(),
];
}
Fill Using
You can use fillUsing to change the pivot-columns values; Then you need to return an associative array that matches your pivot table.
Be careful; the "fillUsing" method applies to each attachment. see the following example:
->fillUsing(function($pivots) {
if(isset($pivots['options']) && is_array($pivots['options'])) {
$pivots['options'] = json_encode($pivots['options']);
}
return $pivots;
}),
统计信息
- 总下载量: 201
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-13