Last active
March 20, 2024 07:25
-
-
Save sjardim/aa5eae42f9206f1d7ab929f9678f64f9 to your computer and use it in GitHub Desktop.
Filament model with dependant selects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Filament\Resources; | |
use App\Filament\Resources\UsersResource\Pages; | |
use App\Filament\Resources\UsersResource\RelationManagers; | |
use App\Models\Company; | |
use App\Models\Folder; | |
use App\Models\User; | |
use App\Models\Users; | |
use Filament\Forms; | |
use Filament\Forms\Components\Actions\Action; | |
use Filament\Forms\Components\Select; | |
use Filament\Forms\Form; | |
use Filament\Forms\Get; | |
use Filament\Resources\Resource; | |
use Filament\Tables; | |
use Filament\Tables\Table; | |
use Illuminate\Database\Eloquent\Builder; | |
use Illuminate\Database\Eloquent\Collection; | |
use Illuminate\Database\Eloquent\SoftDeletingScope; | |
use function dump; | |
class UsersResource extends Resource | |
{ | |
protected static ?string $model = User::class; | |
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; | |
public static function form(Form $form): Form | |
{ | |
return $form | |
->schema([ | |
Forms\Components\TextInput::make('name') | |
->label('Nome') | |
->required(), | |
Forms\Components\TextInput::make('email') | |
->required(), | |
Forms\Components\Select::make('company_id') | |
->relationship('companies', 'name') | |
->label('Empresas') | |
->live() | |
->suffixAction( | |
fn () => Action::make('changeFolders') | |
->icon('heroicon-o-pencil-square') | |
->fillForm(fn (Get $get): array => [ | |
'company_id' => $get('company_id') ?? null | |
]) | |
->form( | |
[ | |
Forms\Components\Select::make('company_id') | |
->label('Empresa') | |
->options(fn (Get $get): \Illuminate\Support\Collection => Company::query() | |
->where('id', $get('company_id')) | |
->pluck('name', 'id')), | |
Forms\Components\Select::make('folder') | |
->options(fn (): \Illuminate\Support\Collection => Folder::query() | |
->where('parent_id', null) | |
->pluck('name', 'id')) | |
->live(), | |
Forms\Components\Select::make('subfolder') | |
->options(fn (Get $get): \Illuminate\Support\Collection => Folder::query() | |
->where('parent_id', $get('folder')) | |
->pluck('name', 'id')) | |
->live(), | |
Forms\Components\Select::make('subsubfolder') | |
->options(fn (Get $get): \Illuminate\Support\Collection => Folder::query() | |
->where('parent_id', $get('subfolder')) | |
->pluck('name', 'id')) | |
] | |
) | |
->action(function (array $data, User $record) { | |
return dump($data); | |
})->modalWidth('md') | |
), | |
]); | |
} | |
public static function table(Table $table): Table | |
{ | |
return $table | |
->columns([ | |
Tables\Columns\TextColumn::make('name'), | |
]) | |
->filters([ | |
// | |
]) | |
->actions([ | |
Tables\Actions\EditAction::make(), | |
]) | |
->bulkActions([ | |
Tables\Actions\BulkActionGroup::make([ | |
Tables\Actions\DeleteBulkAction::make(), | |
]), | |
]); | |
} | |
public static function getRelations(): array | |
{ | |
return [ | |
// | |
]; | |
} | |
public static function getPages(): array | |
{ | |
return [ | |
'index' => Pages\ListUsers::route('/'), | |
'create' => Pages\CreateUsers::route('/create'), | |
'edit' => Pages\EditUsers::route('/{record}/edit'), | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment