-$label - label, field header,
-$relationName - name of the relationship,
-$resource - the model resource referenced by the relation.
The $formatted parameter is not used in the HasMany field!
The presence of the model resource referenced by the relation is mandatory
The resource also needs to be registered with the service provider MoonShineServiceProvider in the method menu() or resources(). Otherwise, there will be a 404 error.
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', 'comments', resource: new CommentResource())
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', 'comments', resource: new CommentResource())
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', 'comments', resource: new CommentResource())
];
}
//...
If you do not specify $relationName, then the relation name will be determined automatically based on $label.
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
];
}
//...
You can omit $resource if the model resource matches the name of the relationship.
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', 'comments')
];
}
useMoonShine\Fields\Relationships\HasMany;
//...
publicfunctionfields():array
{
return [
HasMany::make('Comments', 'comments')
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', 'comments')
];
}
//...
useMoonShine\Fields\Relationships\HasMany;
//...
publicfunctionfields():array
{
return [
HasMany::make('Comments', 'comments')
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', 'comments')
];
}
//...
The fields() method allows you to set the fields that will be displayed in the preview.
fields(Fields|Closure|array $fields)
fields(Fields|Closure|array $fields)
fields(Fields|Closure|array $fields)
fields(Fields|Closure|array $fields)
fields(Fields|Closure|array $fields)
use MoonShine\Fields\Relationships\BelongsTo;
use MoonShine\Fields\Relationships\HasMany;
use MoonShine\Fields\Text;
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->fields([
BelongsTo::make('User'),
Text::make('Text'),
])
];
}
use MoonShine\Fields\Relationships\BelongsTo;
use MoonShine\Fields\Relationships\HasMany;
use MoonShine\Fields\Text;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->fields([
BelongsTo::make('User'),
Text::make('Text'),
])
];
}
//...
use MoonShine\Fields\Relationships\BelongsTo;
use MoonShine\Fields\Relationships\HasMany;
use MoonShine\Fields\Text;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->fields([
BelongsTo::make('User'),
Text::make('Text'),
])
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->creatable()
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->creatable()
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->creatable()
];
}
//...
You can customize the create button by passing the button parameter to the method.
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->creatable(
button: ActionButton::make('Custom button', '')
)
];
}
The limit() method allows you to limit the number of records displayed in preview.
limit(int $limit)
limit(int $limit)
limit(int $limit)
limit(int $limit)
limit(int $limit)
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->limit(1)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->limit(1)
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->limit(1)
];
}
//...
condition - closure or boolean value, responsible for displaying the relationship as a link.
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink()
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink()
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink()
];
}
//...
linkRelation
The linkRelation parameter allows you to create a link to a relation with a parent resource binding.
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink('comment')
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink('comment')
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink('comment')
];
}
//...
condition
The condition parameter via a closure will allow you to change the display method depending on the conditions.
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink(condition: function (int $count, Field $field): bool {
return $count > 10;
})
];
}
->onlyLink(condition: function (int $count, Field $field):bool {
return $count >10;
})
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink(condition: function (int $count, Field $field): bool {
return $count > 10;
})
];
}
//...
->onlyLink(condition: function (int $count, Field $field):bool {
return $count >10;
})
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink(condition: function (int $count, Field $field): bool {
return $count > 10;
})
];
}
//...
If the relationship has a resource, and you want to get the ID of the parent element, then you can use the ResourceWithParent trait.
use MoonShine\Resources\ModelResource;
use MoonShine\Traits\Resource\ResourceWithParent;
class PostImageResource extends ModelResource
{
use ResourceWithParent;
}
useMoonShine\Resources\ModelResource;
useMoonShine\Traits\Resource\ResourceWithParent;
classPostImageResourceextendsModelResource
{
useResourceWithParent;
//...
}
use MoonShine\Resources\ModelResource;
use MoonShine\Traits\Resource\ResourceWithParent;
class PostImageResource extends ModelResource
{
use ResourceWithParent;
//...
}
useMoonShine\Resources\ModelResource;
useMoonShine\Traits\Resource\ResourceWithParent;
classPostImageResourceextendsModelResource
{
useResourceWithParent;
//...
}
use MoonShine\Resources\ModelResource;
use MoonShine\Traits\Resource\ResourceWithParent;
class PostImageResource extends ModelResource
{
use ResourceWithParent;
//...
}
When using a trait, it is necessary to define methods:
protected function getParentResourceClassName(): string
{
return PostResource::class;
}
protected function getParentRelationName(): string
{
return 'post';
}
The HasMany field is displayed outside the main resource form by default. If you need to display relation fields inside the main form, then you can use the JSON field in the asRelation() mode.
public function fields(): array
{
return [
Json::make('Comments', 'comments')
->asRelation(new CommentResource())
]
}
//...
publicfunctionfields():array
{
return [
Json::make('Comments', 'comments')
->asRelation(newCommentResource())
//...
]
}
//...
//...
public function fields(): array
{
return [
Json::make('Comments', 'comments')
->asRelation(new CommentResource())
//...
]
}
//...
//...
publicfunctionfields():array
{
return [
Json::make('Comments', 'comments')
->asRelation(newCommentResource())
//...
]
}
//...
//...
public function fields(): array
{
return [
Json::make('Comments', 'comments')
->asRelation(new CommentResource())
//...
]
}
//...
For more detailed information, please refer to the section Json field.
Relationship via Template field
Using the Template field you can construct a field for HasMany relationships using fluent interface during the declaration process.
For more detailed information, please refer to the section Template field.
HasMany field tabs
In Moonshine you can customize the form page and place HasMany fields in tabs using the Tabs and Tab decorations.