Files
ntspi-app/app/Filament/Resources/SubSectionResource/RelationManagers/PagesRelationManager.php
T
F4ilji c01016a154 Migrate backend to Porto architecture and fix minor bugs
- Fully transitioned the backend to the Porto architectural pattern
- Improved code organization and maintainability
- Fixed minor bugs and inconsistencies
2025-05-12 08:47:43 +05:00

79 lines
3.0 KiB
PHP

<?php
namespace App\Filament\Resources\SubSectionResource\RelationManagers;
use App\Filament\Components\Forms\PageForm;
use App\Containers\AppStructure\Models\Page;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
class PagesRelationManager extends RelationManager
{
protected static string $relationship = 'pages';
protected static ?string $title = 'Страницы';
public function form(Form $form): Form
{
return PageForm::getForm($form);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('title')
->columns([
Tables\Columns\TextColumn::make('title'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
Tables\Actions\Action::make('associate')
->label('Прикрепить страницу')
->color('success')
->button()
->form([
Forms\Components\Select::make('recordId')
->label('Страница')
->searchable()
->preload()
->options(Page::whereNull('sub_section_id')->whereNotNull('title')->pluck('title', 'id'))
->required(),
])
->action(function (array $data): void {
$page = Page::find($data['recordId']);
$page->section()->associate($this->getOwnerRecord());
$page->save(); // Вызовет наблюдатели
}),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\Action::make('detach')
->label('Открепить')
->color('danger')
->icon('heroicon-o-x-mark')
->action(function (Page $record) {
$record->section()->dissociate();
$record->save(); // Вызовет наблюдатели
}),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\Action::make('bulkDetach')
->label('Открепить выбранные')
->color('danger')
->icon('heroicon-o-x-mark')
->action(function ($records) {
$records->each(function (Page $record) {
$record->section()->dissociate();
$record->save(); // Вызовет наблюдатели для каждой записи
});
}),
]),
]);
}
}