schema([ Section::make('') ->schema([ Forms\Components\TextInput::make('name') ->required() // ->unique(ignoreRecord: true) ->placeholder('Scan the valid name') ->autofocus(true) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { $nameId = $get('name'); // Ensure `linestop_id` is not cleared if (!$nameId) { $set('bNameError', 'Scan the valid name.'); return; } else { $set('bNameError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('bNameError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('bNameError') ? $get('bNameError') : null) ->hintColor('danger') ->rule(function (callable $get) { return Rule::unique('blocks', 'name') ->where('plant_id', $get('plant_id')) ->ignore($get('id')); // Ignore current record during updates }), Forms\Components\Select::make('plant_id') ->relationship('plant', 'name') // ->unique(ignoreRecord: true) ->required() ->reactive() ->default(function () { return optional(Block::latest()->first())->plant_id; }) ->disabled(fn (Get $get) => !empty($get('id'))) ->afterStateUpdated(function ($state, callable $set, callable $get) { $nameId = $get('plant_id'); // Ensure `linestop_id` is not cleared if (!$nameId) { $set('bPlantError', 'Please select a plant first.'); return; } else { $set('bPlantError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('bPlantError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('bPlantError') ? $get('bPlantError') : null) ->hintColor('danger'), Forms\Components\TextInput::make('id') ->hidden() ->readOnly(), ]) ->columns(2), ]); } public static function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('id') ->label('ID') ->numeric() ->sortable(), Tables\Columns\TextColumn::make('name') //->unique(ignoreRecord: true) ->sortable(), Tables\Columns\TextColumn::make('plant.name') ->sortable(), Tables\Columns\TextColumn::make('created_at') ->dateTime() ->sortable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('updated_at') ->dateTime() ->sortable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('deleted_at') ->dateTime() ->sortable() ->toggleable(isToggledHiddenByDefault: true), ]) ->filters([ Tables\Filters\TrashedFilter::make(), ]) ->actions([ Tables\Actions\ViewAction::make(), Tables\Actions\EditAction::make(), ]) ->bulkActions([ Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), Tables\Actions\ForceDeleteBulkAction::make(), Tables\Actions\RestoreBulkAction::make(), ]), ]) ->headerActions([ ImportAction::make() ->importer(BlockImporter::class), ]); } public static function getRelations(): array { return [ // ]; } public static function getPages(): array { return [ 'index' => Pages\ListBlocks::route('/'), 'create' => Pages\CreateBlock::route('/create'), 'view' => Pages\ViewBlock::route('/{record}'), 'edit' => Pages\EditBlock::route('/{record}/edit'), ]; } public static function getEloquentQuery(): Builder { return parent::getEloquentQuery() ->withoutGlobalScopes([ SoftDeletingScope::class, ]); } }