How can I access relationships inside the disableOptionWhen

How can I access relationships inside the disableOptionWhen callback exactly the same way I do in getOptionLabelFromRecordUsing? Perhaps I’m approaching this the wrong way. I just want to disable the shift options when the shift->orders relationship has a status other than OrderStatus::New, OrderStatus::Completed, or OrderStatus::Cancelled. But the disableOptionWhen method doesn’t let me access $record->orders; it’s using the Order form model instead of the Shift relationship.

Forms\Components\Select::make('shift_id')
                    ->label(__('Select a shift'))
                    ->relationship('shift', 'id', function ($query) {
                        return $query->where('status', ShiftStatus::Active)->with(['driver', 'car', 'orders']);
                    })
                    ->getOptionLabelFromRecordUsing(
                        fn ($record) =>
                            "{$record->driver->first_name} {$record->driver->last_name}" .
                            " - " .
                            "{$record->car->brand} {$record->car->model}"
                    )
                    ->disableOptionWhen(function ($record) {
//Not Working (((                        
return $record->orders
                            ->whereNotIn('status', [
                                OrderStatus::New,
                                OrderStatus::Completed,
                                OrderStatus::Cancelled,
                            ])
                            ->isNotEmpty();
                    })
                    ->searchable()
Was this page helpful?