dynamic elements and groups
This commit is contained in:
1 parent
80db5b6c6d
commit
0c3142f2fd
42 files changed
+798
-17
No files matched your search
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Elements\Tables;
|
||||
|
||||
use App\Models\CompetitionGroup;
|
||||
use App\Models\Country;
|
||||
use App\Models\ElementGroup;
|
||||
use App\Models\ElementProvider;
|
||||
use App\Models\Event;
|
||||
use App\Models\Sport;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Enums\FiltersLayout;
|
||||
use Filament\Tables\Filters\Filter;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class ElementsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
IconColumn::make('sport.icon')
|
||||
->label('')
|
||||
->grow(false)
|
||||
->color('primary')
|
||||
->width(30)
|
||||
->icon(fn ($record) => $record->sport->icon)
|
||||
->tooltip(fn ($record) => $record->id)
|
||||
->visibleFrom('lg'),
|
||||
TextColumn::make('country.name')
|
||||
->wrap()
|
||||
->color('gray')
|
||||
->icon(fn ($record) => $record->country->icon),
|
||||
TextColumn::make('element_group.competition_group.name')->label('Competition Group'),
|
||||
// TextColumn::make('element_group.group.name')->label('Group'),
|
||||
TextColumn::make('name')->searchable(),
|
||||
])->filters([
|
||||
SelectFilter::make('sport_id')
|
||||
->label('Sport')
|
||||
->options(Sport::where('is_active', 1)->pluck('name', 'id')),
|
||||
Filter::make('competition_group')
|
||||
->columns(3)->columnSpan(3)
|
||||
->schema([
|
||||
Select::make('country_id')
|
||||
->searchable()
|
||||
->label('Country')
|
||||
->options(Country::orderBy('name')->pluck('name', 'id'))
|
||||
->live(),
|
||||
Select::make('competition_group_id')
|
||||
->searchable()
|
||||
->label('Competition Group')
|
||||
->options(function (Get $get) {
|
||||
if ($get('country_id')) {
|
||||
return CompetitionGroup::where('country_id', $get('country_id'))->orderBy('name')->pluck('name', 'id');
|
||||
} else {
|
||||
return CompetitionGroup::orderBy('name')->pluck('name', 'id');
|
||||
}
|
||||
}),
|
||||
])
|
||||
->query(function (Builder $query, array $data): Builder {
|
||||
return $query->when(
|
||||
$data['country_id'],
|
||||
fn (Builder $query, $country_id) => $query->where('country_id', $country_id)
|
||||
)->when(
|
||||
$data['competition_group_id'],
|
||||
function (Builder $query, $competition_group_id) {
|
||||
return $query->whereHas('element_group', function (Builder $query) use ($competition_group_id) {
|
||||
return $query->where('competition_group_id', $competition_group_id);
|
||||
});
|
||||
}
|
||||
);
|
||||
}),
|
||||
], layout: FiltersLayout::AboveContent)
|
||||
->deferFilters(false)
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
DeleteAction::make()
|
||||
->tooltip('All elements connected to this group, including active events, will be deleted')
|
||||
->before(function ($record) {
|
||||
Event::where('home_id', $record->id)->delete();
|
||||
Event::where('away_id', $record->id)->delete();
|
||||
ElementProvider::where('element_id', $record->id)->delete();
|
||||
ElementGroup::where('element_id', $record->id)->delete();
|
||||
})
|
||||
->successNotification(
|
||||
Notification::make()
|
||||
->success()
|
||||
->title('Element deleted')
|
||||
->body('The element with all relations in other tables has been deleted successfully.'),
|
||||
),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user