TitleAttribute doesn't appear in Form Select

What I am trying to do: I am trying to model an optional one-to-one relationship. What I did: I created a resource for Test and in a select input, I specified to only bring zero_ones that are not yet related to a test. My issue: When searching and relating it for the first time, it works, but when I go to edit an already related one, the titleAttribute does not appear, only the id does. I assume this is because it's not fetching the current or the same one. How can I retrieve the title of the currently selected one, or how should I handle this? Thank you in advance. Code:
class TestResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('data')
->required()
->maxLength(255),
Forms\Components\Select::make('zero_one_id')
->relationship( // optional
name: 'zeroOne', titleAttribute: 'data',
modifyQueryUsing: fn(Builder $query)
=> $query->doesntHave('test'),
)
->searchable()
->createOptionForm([
Forms\Components\TextInput::make('data')
->required()
->maxLength(255),
]),
// Another attempt that didn't work:
// Section::make('Zero One')
// ->headerActions([])
// ->relationship('zeroOne')
// ->schema([
// Forms\Components\TextInput::make('data')
// ->required()
// ->maxLength(255),
// ])
]);
}
// ...
}
class TestResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('data')
->required()
->maxLength(255),
Forms\Components\Select::make('zero_one_id')
->relationship( // optional
name: 'zeroOne', titleAttribute: 'data',
modifyQueryUsing: fn(Builder $query)
=> $query->doesntHave('test'),
)
->searchable()
->createOptionForm([
Forms\Components\TextInput::make('data')
->required()
->maxLength(255),
]),
// Another attempt that didn't work:
// Section::make('Zero One')
// ->headerActions([])
// ->relationship('zeroOne')
// ->schema([
// Forms\Components\TextInput::make('data')
// ->required()
// ->maxLength(255),
// ])
]);
}
// ...
}
Tables: zero_ones(id, data) tests(id, zero_one_id [fk, nullable, unique, on delete set null], data) Models:
class Test extends Model {
// ...
public function zeroOne(): BelongsTo {
return $this->belongsTo(ZeroOne::class);
}
}
class ZeroOne extends Model {
// ...
public function test(): HasOne {
return $this->hasOne(Test::class);
}
}
class Test extends Model {
// ...
public function zeroOne(): BelongsTo {
return $this->belongsTo(ZeroOne::class);
}
}
class ZeroOne extends Model {
// ...
public function test(): HasOne {
return $this->hasOne(Test::class);
}
}
No description
No description
2 Replies
LeandroFerreira
LeandroFerreira4mo ago
I think you need to add this modifyQueryUsing only in the create page https://discord.com/channels/883083792112300104/1263858697151447121/1263869505356959775
demonshide
demonshideOP4mo ago
I found out I can use $record to access the current resource and successfully updated the query. I'm really sorry for the trouble and thank you so much for your help.
Want results from more Discord servers?
Add your server