Поле HasMany предназначено для работы с отношением того же имени в Laravel и включает все базовые методы.
Для создания этого поля используйте статический метод make()
.
HasMany::make(
Closure|string $label,
?string $relationName = null,
Closure|string|null $formatted = null,
?ModelResource $resource = null
HasMany::make(
Closure|string $label,
?string $relationName = null,
Closure|string|null $formatted = null,
?ModelResource $resource = null
HasMany::make(
Closure|string $label,
?string $relationName = null,
Closure|string|null $formatted = null,
?ModelResource $resource = null
HasMany::make(
Closure|string $label,
?string $relationName = null,
Closure|string|null $formatted = null,
?ModelResource $resource = null
HasMany::make(
Closure|string $label,
?string $relationName = null,
Closure|string|null $formatted = null,
?ModelResource $resource = null
-$label
- метка, заголовок поля,
-$relationName
- имя отношения,
-$resource
- ресурс модели, на который ссылается отношение.
Параметр $formatted
не используется в поле HasMany
!
Наличие ресурса модели, на который ссылается отношение, обязательно.
Ресурс также необходимо зарегистрировать в сервис-провайдере MoonShineServiceProvider
в методе menu()
или resources()
. В противном случае будет ошибка 404.
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())
];
}
//...
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())
];
}
//...

Если вы не указываете $relationName
, тогда имя отношения будет определено автоматически на основе $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())
];
}
//...
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())
];
}
//...
Вы можете опустить $resource
, если ресурс модели соответствует имени отношения.
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', 'comments')
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', 'comments')
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', 'comments')
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', 'comments')
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', 'comments')
];
}
//...
Метод fields()
позволяет установить поля, которые будут отображаться в 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\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'),
])
];
}
//...

Метод creatable()
позволяет создать новый объект отношения через модальное окно.
creatable(
Closure|bool|null $condition = null,
?ActionButton $button = null,
)
creatable(
Closure|bool|null $condition = null,
?ActionButton $button = null,
)
creatable(
Closure|bool|null $condition = null,
?ActionButton $button = null,
)
creatable(
Closure|bool|null $condition = null,
?ActionButton $button = null,
)
creatable(
Closure|bool|null $condition = null,
?ActionButton $button = null,
)
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()
];
}
//...
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()
];
}
//...

Вы можете настроить кнопку создания, передав параметр button в метод.
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->creatable(
button: ActionButton::make('Custom button', '')
)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->creatable(
button: ActionButton::make('Custom button', '')
)
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->creatable(
button: ActionButton::make('Custom button', '')
)
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->creatable(
button: ActionButton::make('Custom button', '')
)
];
}
//...
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->creatable(
button: ActionButton::make('Custom button', '')
)
];
}
//...
Метод limit()
позволяет ограничить количество записей, отображаемых в 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)
];
}
//...
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)
];
}
//...
Метод onlyLink()
позволит отобразить отношение в виде ссылки с количеством элементов.
onlyLink(?string $linkRelation = null, Closure|bool|null $condition = null)
onlyLink(?string $linkRelation = null, Closure|bool|null $condition = null)
onlyLink(?string $linkRelation = null, Closure|bool|null $condition = null)
onlyLink(?string $linkRelation = null, Closure|bool|null $condition = null)
onlyLink(?string $linkRelation = null, Closure|bool|null $condition = null)
Вы можете передать в метод необязательные параметры:
linkRelation
- ссылка на отношение;
condition
- замыкание или булево значение, отвечающее за отображение отношения в виде ссылки.
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()
];
}
//...
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
Параметр linkRelation
позволяет создать ссылку на отношение с привязкой родительского ресурса.
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')
];
}
//...
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
Параметр condition
через замыкание позволит изменить метод отображения в зависимости от условий.
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;
})
];
}
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;
})
];
}
//...
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;
})
];
}
//...
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;
})
];
}
//...
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;
})
];
}
//...
Если у отношения есть ресурс, и вы хотите получить ID родительского элемента, то вы можете использовать трейт ResourceWithParent.
use MoonShine\Resources\ModelResource;
use MoonShine\Traits\Resource\ResourceWithParent;
class PostImageResource extends ModelResource
{
use ResourceWithParent;
}
use MoonShine\Resources\ModelResource;
use MoonShine\Traits\Resource\ResourceWithParent;
class PostImageResource extends ModelResource
{
use ResourceWithParent;
//...
}
use MoonShine\Resources\ModelResource;
use MoonShine\Traits\Resource\ResourceWithParent;
class PostImageResource extends ModelResource
{
use ResourceWithParent;
//...
}
use MoonShine\Resources\ModelResource;
use MoonShine\Traits\Resource\ResourceWithParent;
class PostImageResource extends ModelResource
{
use ResourceWithParent;
//...
}
use MoonShine\Resources\ModelResource;
use MoonShine\Traits\Resource\ResourceWithParent;
class PostImageResource extends ModelResource
{
use ResourceWithParent;
//...
}
При использовании трейта необходимо определить методы:
protected function getParentResourceClassName(): string
{
return PostResource::class;
}
protected function getParentRelationName(): string
{
return 'post';
}
protected function getParentResourceClassName(): string
{
return PostResource::class;
}
protected function getParentRelationName(): string
{
return 'post';
}
protected function getParentResourceClassName(): string
{
return PostResource::class;
}
protected function getParentRelationName(): string
{
return 'post';
}
protected function getParentResourceClassName(): string
{
return PostResource::class;
}
protected function getParentRelationName(): string
{
return 'post';
}
protected function getParentResourceClassName(): string
{
return PostResource::class;
}
protected function getParentRelationName(): string
{
return 'post';
}
Для получения ID родителя используйте метод getParentId()
.
$this->getParentId();
$this->getParentId();
$this->getParentId();
$this->getParentId();
$this->getParentId();
Метод changeEditButton()
позволяет полностью переопределить кнопку редактирования.
use MoonShine\Fields\Relationships\HasMany;
HasMany::make('Comments', 'comments', resource: new CommentResource())
->changeEditButton(
ActionButton::make(
'Edit',
fn(Comment $comment) => (new CommentResource())->formPageUrl($comment)
)
)
use MoonShine\Fields\Relationships\HasMany;
//...
HasMany::make('Comments', 'comments', resource: new CommentResource())
->changeEditButton(
ActionButton::make(
'Edit',
fn(Comment $comment) => (new CommentResource())->formPageUrl($comment)
)
)
use MoonShine\Fields\Relationships\HasMany;
//...
HasMany::make('Comments', 'comments', resource: new CommentResource())
->changeEditButton(
ActionButton::make(
'Edit',
fn(Comment $comment) => (new CommentResource())->formPageUrl($comment)
)
)
use MoonShine\Fields\Relationships\HasMany;
//...
HasMany::make('Comments', 'comments', resource: new CommentResource())
->changeEditButton(
ActionButton::make(
'Edit',
fn(Comment $comment) => (new CommentResource())->formPageUrl($comment)
)
)
use MoonShine\Fields\Relationships\HasMany;
//...
HasMany::make('Comments', 'comments', resource: new CommentResource())
->changeEditButton(
ActionButton::make(
'Edit',
fn(Comment $comment) => (new CommentResource())->formPageUrl($comment)
)
)
По умолчанию создание и редактирование записи поля HasMany происходит в модальном окне, метод withoutModals()
позволяет отключить это поведение.
use MoonShine\Fields\Relationships\HasMany;
HasMany::make('Comments', 'comments', resource: new CommentResource())
->withoutModals()
use MoonShine\Fields\Relationships\HasMany;
//...
HasMany::make('Comments', 'comments', resource: new CommentResource())
->withoutModals()
use MoonShine\Fields\Relationships\HasMany;
//...
HasMany::make('Comments', 'comments', resource: new CommentResource())
->withoutModals()
use MoonShine\Fields\Relationships\HasMany;
//...
HasMany::make('Comments', 'comments', resource: new CommentResource())
->withoutModals()
use MoonShine\Fields\Relationships\HasMany;
//...
HasMany::make('Comments', 'comments', resource: new CommentResource())
->withoutModals()
Поле HasMany имеет методы, которые можно использовать для модификации кнопок, изменения TableBuilder для предпросмотра и формы, а также изменения кнопки onlyLink.
modifyItemButtons()
Метод modifyItemButtons()
позволяет изменить кнопки просмотра, редактирования, удаления и массового удаления.
modifyItemButtons(Closure $callback)
/**
* @param Closure(ActionButton $detail, ActionButton $edit, ActionButton $delete, ActionButton $massDelete, self $field): array $callback
*/
modifyItemButtons(Closure $callback)
/**
* @param Closure(ActionButton $detail, ActionButton $edit, ActionButton $delete, ActionButton $massDelete, self $field): array $callback
*/
modifyItemButtons(Closure $callback)
/**
* @param Closure(ActionButton $detail, ActionButton $edit, ActionButton $delete, ActionButton $massDelete, self $field): array $callback
*/
modifyItemButtons(Closure $callback)
/**
* @param Closure(ActionButton $detail, ActionButton $edit, ActionButton $delete, ActionButton $massDelete, self $field): array $callback
*/
modifyItemButtons(Closure $callback)
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyItemButtons(
fn(ActionButton $detail, $edit, $delete, $massDelete, HasMany $ctx) => [$detail]
)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyItemButtons(
fn(ActionButton $detail, $edit, $delete, $massDelete, HasMany $ctx) => [$detail]
)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyItemButtons(
fn(ActionButton $detail, $edit, $delete, $massDelete, HasMany $ctx) => [$detail]
)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyItemButtons(
fn(ActionButton $detail, $edit, $delete, $massDelete, HasMany $ctx) => [$detail]
)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyItemButtons(
fn(ActionButton $detail, $edit, $delete, $massDelete, HasMany $ctx) => [$detail]
)
];
}
modifyOnlyLinkButton()
Метод modifyOnlyLinkButton()
позволяет изменить кнопку onlyLink.
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink()
->modifyOnlyLinkButton(
fn(ActionButton $button, bool $preview) => $button
->when($preview, fn(ActionButton $btn) => $btn->primary())
->unless($preview, fn(ActionButton $btn) => $btn->secondary())
)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink()
->modifyOnlyLinkButton(
fn(ActionButton $button, bool $preview) => $button
->when($preview, fn(ActionButton $btn) => $btn->primary())
->unless($preview, fn(ActionButton $btn) => $btn->secondary())
)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink()
->modifyOnlyLinkButton(
fn(ActionButton $button, bool $preview) => $button
->when($preview, fn(ActionButton $btn) => $btn->primary())
->unless($preview, fn(ActionButton $btn) => $btn->secondary())
)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink()
->modifyOnlyLinkButton(
fn(ActionButton $button, bool $preview) => $button
->when($preview, fn(ActionButton $btn) => $btn->primary())
->unless($preview, fn(ActionButton $btn) => $btn->secondary())
)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->onlyLink()
->modifyOnlyLinkButton(
fn(ActionButton $button, bool $preview) => $button
->when($preview, fn(ActionButton $btn) => $btn->primary())
->unless($preview, fn(ActionButton $btn) => $btn->secondary())
)
];
}
modifyCreateButton() / modifyEditButton()
Методы modifyCreateButton()
и modifyEditButton()
позволяют изменить кнопки создания и редактирования.
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyCreateButton(
fn(ActionButton $button) => $button->setLabel('Custom create button')
)
->modifyEditButton(
fn(ActionButton $button) => $button->setLabel('Custom edit button')
)
->creatable(true)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyCreateButton(
fn(ActionButton $button) => $button->setLabel('Custom create button')
)
->modifyEditButton(
fn(ActionButton $button) => $button->setLabel('Custom edit button')
)
->creatable(true)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyCreateButton(
fn(ActionButton $button) => $button->setLabel('Custom create button')
)
->modifyEditButton(
fn(ActionButton $button) => $button->setLabel('Custom edit button')
)
->creatable(true)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyCreateButton(
fn(ActionButton $button) => $button->setLabel('Custom create button')
)
->modifyEditButton(
fn(ActionButton $button) => $button->setLabel('Custom edit button')
)
->creatable(true)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyCreateButton(
fn(ActionButton $button) => $button->setLabel('Custom create button')
)
->modifyEditButton(
fn(ActionButton $button) => $button->setLabel('Custom edit button')
)
->creatable(true)
];
}
modifyTable()
Метод modifyTable()
позволяет изменить TableBuilder для предпросмотра и формы.
use MoonShine\Fields\Relationships\HasMany;
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyTable(
fn(TableBuilder $table, bool $preview) => $table
->when($preview, fn(TableBuilder $tbl) => $tbl->customAttributes(['style' => 'background: blue']))
->unless($preview, fn(TableBuilder $tbl) => $tbl->customAttributes(['style' => 'background: green']))
)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyTable(
fn(TableBuilder $table, bool $preview) => $table
->when($preview, fn(TableBuilder $tbl) => $tbl->customAttributes(['style' => 'background: blue']))
->unless($preview, fn(TableBuilder $tbl) => $tbl->customAttributes(['style' => 'background: green']))
)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyTable(
fn(TableBuilder $table, bool $preview) => $table
->when($preview, fn(TableBuilder $tbl) => $tbl->customAttributes(['style' => 'background: blue']))
->unless($preview, fn(TableBuilder $tbl) => $tbl->customAttributes(['style' => 'background: green']))
)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyTable(
fn(TableBuilder $table, bool $preview) => $table
->when($preview, fn(TableBuilder $tbl) => $tbl->customAttributes(['style' => 'background: blue']))
->unless($preview, fn(TableBuilder $tbl) => $tbl->customAttributes(['style' => 'background: green']))
)
];
}
use MoonShine\Fields\Relationships\HasMany;
//...
public function fields(): array
{
return [
HasMany::make('Comments', resource: new CommentResource())
->modifyTable(
fn(TableBuilder $table, bool $preview) => $table
->when($preview, fn(TableBuilder $tbl) => $tbl->customAttributes(['style' => 'background: blue']))
->unless($preview, fn(TableBuilder $tbl) => $tbl->customAttributes(['style' => 'background: green']))
)
];
}
Отношение через JSON поле
Поле HasMany по умолчанию отображается вне основной формы ресурса. Если вам нужно отобразить поля отношения внутри основной формы, то вы можете использовать поле JSON в режиме asRelation()
.
public function fields(): array
{
return [
Json::make('Comments', 'comments')
->asRelation(new CommentResource())
]
}
//...
public function fields(): array
{
return [
Json::make('Comments', 'comments')
->asRelation(new CommentResource())
//...
]
}
//...
//...
public function fields(): array
{
return [
Json::make('Comments', 'comments')
->asRelation(new CommentResource())
//...
]
}
//...
//...
public function fields(): array
{
return [
Json::make('Comments', 'comments')
->asRelation(new CommentResource())
//...
]
}
//...
//...
public function fields(): array
{
return [
Json::make('Comments', 'comments')
->asRelation(new CommentResource())
//...
]
}
//...
Для более подробной информации обратитесь к разделу Json поле.
Отношение через поле Template
Используя поле Template, вы можете построить поле для отношений HasMany, используя fluent интерфейс в процессе декларации.
Для более подробной информации обратитесь к разделу Поле Template.
Вкладки поля HasMany
В Moonshine вы можете настроить страницу формы и разместить поля HasMany во вкладках, используя декорации Tabs и Tab.
class PostFormPage extends FormPage
{
public function components(): array
{
if(! $this->getResource()->getItemID()) {
return parent::components();
}
$bottomComponents = $this->getLayerComponents(Layer::BOTTOM);
$imagesComponent = collect($bottomComponents)->filter(fn($component) => $component->getName() === 'images')->first();
$commentsComponent = collect($bottomComponents)->filter(fn($component) => $component->getName() === 'comments')->first();
$tabLayer = [
Block::make('', [
Tabs::make([
Tab::make('Редактировать', $this->mainLayer()),
Tab::make('Изображения', [$imagesComponent]),
Tab::make('Комментарии', [$commentsComponent])
])
])
];
return [
...$this->getLayerComponents(Layer::TOP),
...$tabLayer,
];
}
}
class PostFormPage extends FormPage
{
public function components(): array
{
if(! $this->getResource()->getItemID()) {
return parent::components();
}
$bottomComponents = $this->getLayerComponents(Layer::BOTTOM);
$imagesComponent = collect($bottomComponents)->filter(fn($component) => $component->getName() === 'images')->first();
$commentsComponent = collect($bottomComponents)->filter(fn($component) => $component->getName() === 'comments')->first();
$tabLayer = [
Block::make('', [
Tabs::make([
Tab::make('Редактировать', $this->mainLayer()),
Tab::make('Изображения', [$imagesComponent]),
Tab::make('Комментарии', [$commentsComponent])
])
])
];
return [
...$this->getLayerComponents(Layer::TOP),
...$tabLayer,
];
}
}
class PostFormPage extends FormPage
{
public function components(): array
{
if(! $this->getResource()->getItemID()) {
return parent::components();
}
$bottomComponents = $this->getLayerComponents(Layer::BOTTOM);
$imagesComponent = collect($bottomComponents)->filter(fn($component) => $component->getName() === 'images')->first();
$commentsComponent = collect($bottomComponents)->filter(fn($component) => $component->getName() === 'comments')->first();
$tabLayer = [
Block::make('', [
Tabs::make([
Tab::make('Редактировать', $this->mainLayer()),
Tab::make('Изображения', [$imagesComponent]),
Tab::make('Комментарии', [$commentsComponent])
])
])
];
return [
...$this->getLayerComponents(Layer::TOP),
...$tabLayer,
];
}
}
class PostFormPage extends FormPage
{
public function components(): array
{
if(! $this->getResource()->getItemID()) {
return parent::components();
}
$bottomComponents = $this->getLayerComponents(Layer::BOTTOM);
$imagesComponent = collect($bottomComponents)->filter(fn($component) => $component->getName() === 'images')->first();
$commentsComponent = collect($bottomComponents)->filter(fn($component) => $component->getName() === 'comments')->first();
$tabLayer = [
Block::make('', [
Tabs::make([
Tab::make('Редактировать', $this->mainLayer()),
Tab::make('Изображения', [$imagesComponent]),
Tab::make('Комментарии', [$commentsComponent])
])
])
];
return [
...$this->getLayerComponents(Layer::TOP),
...$tabLayer,
];
}
}
class PostFormPage extends FormPage
{
public function components(): array
{
if(! $this->getResource()->getItemID()) {
return parent::components();
}
$bottomComponents = $this->getLayerComponents(Layer::BOTTOM);
$imagesComponent = collect($bottomComponents)->filter(fn($component) => $component->getName() === 'images')->first();
$commentsComponent = collect($bottomComponents)->filter(fn($component) => $component->getName() === 'comments')->first();
$tabLayer = [
Block::make('', [
Tabs::make([
Tab::make('Редактировать', $this->mainLayer()),
Tab::make('Изображения', [$imagesComponent]),
Tab::make('Комментарии', [$commentsComponent])
])
])
];
return [
...$this->getLayerComponents(Layer::TOP),
...$tabLayer,
];
}
}