armincms / many-to-many
A Laravel Nova field.
Installs: 51 331
Dependents: 5
Suggesters: 0
Security: 0
Stars: 24
Watchers: 2
Forks: 14
Open Issues: 15
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
A Laravel Nova field for polymorphic and non-polymorphic ManyToMany
relationships.
Table of Contents
- Features
- Install
- Simple Usage
- Searching
- Pivots
- Duplicate Attachment
- Polymorphic Relation
- Fill Using
- Filter Related Resources
Features
- Attach polymorphic and non-polymorphic
ManyToMany
relationships in the creation and update page - Edit pivot columns when attaching relation
- Attach a source to another resource many times
Install
composer require armincms/many-to-many
Simple Usage
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(),
];
}
Searching
To search relation value instead of select it; you can use the searchable
method on the field.
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.
In addition to the pivot columns, the pivot table should have its own id
column which must be mentioned in the model definition.
<?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 Attachment
You can use the duplicate
feature for repetitively attach a resource to another resource. follow the example:
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(),
];
}
Polymorphic Relation
Using for the polymorphic relationships is like non-polymorphic. follow the example:
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(),
];
}
or
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(),
];
}
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;
}),
Filter Related Resources
You can use withAttachableFilters
to add filters to get related resource options based on related resource attributes. See the following example:
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']),
];
}