Use value from parent model as default in attachAction relationmanager

I have a Service Model and a Doctor Model, and a ServiceDoctor Pivot Table. I am using a relation manager to attach Doctors to Services. The Pivot Table also has some extra fields like price and time. The service table also have fields named default_price and default_time I want to attach action to set the default values during attachment. Is this possible?
class DoctorRelationManager extends RelationManager
{
protected static string $relationship = 'doctors';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('first_name')
->required()
->maxLength(255),
]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('first_name')
->columns([
Tables\Columns\TextColumn::make('first_name'),
Tables\Columns\TextColumn::make('price'),
Tables\Columns\TextColumn::make('price_online'),
Tables\Columns\TextColumn::make('time'),
])
->filters([
//
])
->headerActions([
Tables\Actions\AttachAction::make()
->preloadRecordSelect()
->form(fn (Tables\Actions\AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\TextInput::make('price')
->numeric()
->required()
->label("Price Offline (0 if not provided)")
->default(function () use ($action) {
return $action->parent?->default_price; // I want the default_price to be pulled from the parent Service class
}),
...
]),
])
->actions([
Tables\Actions\DetachAction::make(),
]);
}
}
class DoctorRelationManager extends RelationManager
{
protected static string $relationship = 'doctors';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('first_name')
->required()
->maxLength(255),
]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('first_name')
->columns([
Tables\Columns\TextColumn::make('first_name'),
Tables\Columns\TextColumn::make('price'),
Tables\Columns\TextColumn::make('price_online'),
Tables\Columns\TextColumn::make('time'),
])
->filters([
//
])
->headerActions([
Tables\Actions\AttachAction::make()
->preloadRecordSelect()
->form(fn (Tables\Actions\AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\TextInput::make('price')
->numeric()
->required()
->label("Price Offline (0 if not provided)")
->default(function () use ($action) {
return $action->parent?->default_price; // I want the default_price to be pulled from the parent Service class
}),
...
]),
])
->actions([
Tables\Actions\DetachAction::make(),
]);
}
}
Solution:
Figured it out, i had to use
->default($this->getOwnerRecord()->default_price),
->default($this->getOwnerRecord()->default_price),
...
Jump to solution
1 Reply
Solution
DarkCoder
DarkCoder7mo ago
Figured it out, i had to use
->default($this->getOwnerRecord()->default_price),
->default($this->getOwnerRecord()->default_price),
Want results from more Discord servers?
Add your server