ModelResource

Кнопки

Скринкасты

Основы

Кнопки на страницах ресурса отвечают за основные действия с элементами и являются компонентами ActionButton.

В MoonShine есть множество методов, позволяющих переопределить у ресурса как отдельные кнопки, так и всю группу.

Более подробная информация о компоненте ActionButton.

Кнопки для создания, просмотра, редактирования, удаления и массового удаления размещены в отдельных классах, чтобы применить к ним все необходимые методы и тем самым устранить дублирование, поскольку эти кнопки также используются в HasMany, BelongsToMany и т.д.

Кнопки индексной страницы

topLeftButtons и topRightButtons

Методы topLeftButtons() и topRightButtons() в классе индексной страницы позволяют добавлять\переопределять кнопки над таблицей.

 namespaces
namespace App\MoonShine\Resources\Post\Pages;
 
use MoonShine\Support\AlpineJs;
use MoonShine\Support\Enums\JsEvent;
use MoonShine\Support\ListOf;
use MoonShine\UI\Components\ActionButton;
use MoonShine\Laravel\Pages\Crud\IndexPage;
 
class PostIndexPage extends IndexPage
{
// ...
 
protected function topLeftButtons(): ListOf
{
return parent::topLeftButtons()
->add(
ActionButton::make('Refresh', '#')
->dispatchEvent(
AlpineJs::event(JsEvent::TABLE_UPDATED, $this->getListComponentName())
)
);
}
}
 namespaces
namespace App\MoonShine\Resources\Post\Pages;
 
use MoonShine\Support\AlpineJs;
use MoonShine\Support\Enums\JsEvent;
use MoonShine\Support\ListOf;
use MoonShine\UI\Components\ActionButton;
use MoonShine\Laravel\Pages\Crud\IndexPage;
 
class PostIndexPage extends IndexPage
{
// ...
 
protected function topLeftButtons(): ListOf
{
return parent::topLeftButtons()
->add(
ActionButton::make('Refresh', '#')
->dispatchEvent(
AlpineJs::event(JsEvent::TABLE_UPDATED, $this->getListComponentName())
)
);
}
}

resource_buttons_actions resource_buttons_actions_dark

Модификация кнопки создания

Метод modifyCreateButton() позволяет модифицировать кнопку создания нового элемента.

 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
 
protected function modifyCreateButton(ActionButtonContract $button): ActionButtonContract
{
return $button->error();
}
 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
 
protected function modifyCreateButton(ActionButtonContract $button): ActionButtonContract
{
return $button->error();
}

Так же, вы можете переопределить кнопку.

 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
use MoonShine\UI\Components\ActionButton;
 
protected function modifyCreateButton(ActionButtonContract $button): ActionButtonContract
{
return ActionButton::make('Create');
}
 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
use MoonShine\UI\Components\ActionButton;
 
protected function modifyCreateButton(ActionButtonContract $button): ActionButtonContract
{
return ActionButton::make('Create');
}

resource_button_create resource_button_create_dark

Модификация кнопки фильтров

Метод modifyFiltersButton() позволяет модифицировать или переопределить кнопку фильтров.

 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
 
protected function modifyFiltersButton(ActionButtonContract $button): ActionButtonContract
{
return $button->error();
}
 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
 
protected function modifyFiltersButton(ActionButtonContract $button): ActionButtonContract
{
return $button->error();
}

resource_button_filters resource_button_filters_dark

Кнопки индексной таблицы

Для добавления\переопределения кнопок в индексной таблице, используйте метод buttons() в классе индексной страницы.

 namespaces
use Illuminate\Database\Eloquent\Model;
use MoonShine\UI\Components\ActionButton;
use MoonShine\Support\ListOf;
 
protected function buttons(): ListOf
{
return parent::buttons()
->prepend(
ActionButton::make(
'Link',
fn(Model $item) => '/endpoint?id=' . $item->getKey()
)
);
}
 namespaces
use Illuminate\Database\Eloquent\Model;
use MoonShine\UI\Components\ActionButton;
use MoonShine\Support\ListOf;
 
protected function buttons(): ListOf
{
return parent::buttons()
->prepend(
ActionButton::make(
'Link',
fn(Model $item) => '/endpoint?id=' . $item->getKey()
)
);
}

resource_buttons_index resource_buttons_index_dark

Для массовых действий с элементами необходимо добавить метод bulk().

 namespaces
use MoonShine\UI\Components\ActionButton;
use MoonShine\Support\ListOf;
 
protected function buttons(): ListOf
{
return parent::buttons()
->prepend(
ActionButton::make('Link', '/endpoint')
->bulk()
);
}
 namespaces
use MoonShine\UI\Components\ActionButton;
use MoonShine\Support\ListOf;
 
protected function buttons(): ListOf
{
return parent::buttons()
->prepend(
ActionButton::make('Link', '/endpoint')
->bulk()
);
}

resource_buttons_bulk resource_buttons_bulk_dark

Модификация кнопки детального просмотра

Метод modifyDetailButton() позволяет модифицировать или переопределить кнопку детального просмотра элемента.

 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
 
protected function modifyDetailButton(ActionButtonContract $button): ActionButtonContract
{
return $button->warning();
}
 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
 
protected function modifyDetailButton(ActionButtonContract $button): ActionButtonContract
{
return $button->warning();
}

resource_button_detail resource_button_detail_dark

Модификация кнопки редактирования

Метод modifyEditButton() позволяет модифицировать или переопределить кнопку редактирования элемента.

 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
 
protected function modifyEditButton(ActionButtonContract $button): ActionButtonContract
{
return $button->icon('pencil-square');
}
 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
 
protected function modifyEditButton(ActionButtonContract $button): ActionButtonContract
{
return $button->icon('pencil-square');
}

resource_button_edit resource_button_edit_dark

Модификация кнопки удаления

Метод modifyDeleteButton() позволяет модифицировать или переопределить кнопку удаления элемента.

 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
 
protected function modifyDeleteButton(ActionButtonContract $button): ActionButtonContract
{
return $button->icon('x-mark');
}
 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
 
protected function modifyDeleteButton(ActionButtonContract $button): ActionButtonContract
{
return $button->icon('x-mark');
}

resource_button_delete resource_button_delete_dark

Модификация кнопки массового удаления

Метод modifyMassDeleteButton() позволяет модифицировать или переопределить кнопку массового удаления.

 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
 
protected function modifyMassDeleteButton(ActionButtonContract $button): ActionButtonContract
{
return $button->icon('x-mark');
}
 namespaces
use MoonShine\Contracts\UI\ActionButtonContract;
 
protected function modifyMassDeleteButton(ActionButtonContract $button): ActionButtonContract
{
return $button->icon('x-mark');
}

resource_button_mass_delete resource_button_mass_delete

Кнопки страницы формы

Чтобы добавить кнопки на страницу с формой, используйте метод buttons() в классе страницы формы.

 namespaces
use MoonShine\UI\Components\ActionButton;
use MoonShine\Support\ListOf;
 
protected function buttons(): ListOf
{
return parent::buttons()
->add(
ActionButton::make('Link')->method('updateSomething')
);
}
 namespaces
use MoonShine\UI\Components\ActionButton;
use MoonShine\Support\ListOf;
 
protected function buttons(): ListOf
{
return parent::buttons()
->add(
ActionButton::make('Link')->method('updateSomething')
);
}

resource_buttons_form resource_buttons_form_dark

Метод formButtons() позволяет добавить\переопределить кнопки непосредственно в форму создания или редактирования.

 namespaces
use MoonShine\UI\Components\ActionButton;
use MoonShine\Support\ListOf;
 
protected function formButtons(): ListOf
{
return parent::formButtons()
->add(
ActionButton::make('Back', fn() => $this->getIndexPageUrl())->class('btn-lg')
);
}
 namespaces
use MoonShine\UI\Components\ActionButton;
use MoonShine\Support\ListOf;
 
protected function formButtons(): ListOf
{
return parent::formButtons()
->add(
ActionButton::make('Back', fn() => $this->getIndexPageUrl())->class('btn-lg')
);
}

resource_buttons_form_builder resource_buttons_form_builder

Кнопки страницы детального просмотра

Чтобы добавить\переопределить кнопки на странице детального просмотра, используйте метод buttons() в классе детальной страницы.

 namespaces
use MoonShine\UI\Components\ActionButton;
use MoonShine\Support\ListOf;
 
protected function buttons(): ListOf
{
return parent::buttons()
->add(ActionButton::make('Link', '/endpoint'));
}
 namespaces
use MoonShine\UI\Components\ActionButton;
use MoonShine\Support\ListOf;
 
protected function buttons(): ListOf
{
return parent::buttons()
->add(ActionButton::make('Link', '/endpoint'));
}

resource_buttons_detail resource_buttons_detail_dark