F
Filamentβ€’8mo ago
cashing

Is there a better way to set the default value in RelationManager?

I currently have a itemsRelationManager where you can see all the items from 1 category. The problem i have is i want to set the default value for category_id when you create a Item in the category. Is there a better way to get the category id? Code:
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('title')
->required()
->label('Titel')
->maxLength(255),
Forms\Components\FileUpload::make('image')
->image()
->imageEditor()
->acceptedFileTypes(['image/png', 'image/jpeg', 'image/webp'])
->rules( 'file', 'mimetypes:image/png,image/jpeg,image/webp')
->maxSize(1024)
->directory('items')
->required(),
Forms\Components\Textarea::make('description')
->label('Beschrijving'),
Forms\Components\ColorPicker::make('background_color')
->label('Achtergrondkleur'),
Forms\Components\Select::make('category_id')
->required()
->relationship('category', 'name')
->default(function (RelationManager $livewire) {
return $livewire->ownerRecord->id;
}),
]);
}
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('title')
->required()
->label('Titel')
->maxLength(255),
Forms\Components\FileUpload::make('image')
->image()
->imageEditor()
->acceptedFileTypes(['image/png', 'image/jpeg', 'image/webp'])
->rules( 'file', 'mimetypes:image/png,image/jpeg,image/webp')
->maxSize(1024)
->directory('items')
->required(),
Forms\Components\Textarea::make('description')
->label('Beschrijving'),
Forms\Components\ColorPicker::make('background_color')
->label('Achtergrondkleur'),
Forms\Components\Select::make('category_id')
->required()
->relationship('category', 'name')
->default(function (RelationManager $livewire) {
return $livewire->ownerRecord->id;
}),
]);
}
No description
No description
Solution:
yeah this: ```php ->default(function (RelationManager $livewire) { return $livewire->getOwnerRecord()->getAttribute('id'); })...
Jump to solution
7 Replies
Ramon.vV
Ramon.vVβ€’8mo ago
I'm using this way myself as well, something wrong with the result you're getting?
cashing
cashingβ€’8mo ago
No but i am just getting a property accessed magic method in my editor error @RamonVveghel i found the fix for it.
Ramon.vV
Ramon.vVβ€’8mo ago
ooh, ->getOwnerRecord() probably?
Solution
cashing
cashingβ€’8mo ago
yeah this:
->default(function (RelationManager $livewire) {
return $livewire->getOwnerRecord()->getAttribute('id');
})
->default(function (RelationManager $livewire) {
return $livewire->getOwnerRecord()->getAttribute('id');
})
Ramon.vV
Ramon.vVβ€’8mo ago
which is what I'm doing yeah. nice!
cashing
cashingβ€’8mo ago
thanks for your input πŸ™‚
Ramon.vV
Ramon.vVβ€’8mo ago

->default(function (RelationManager $livewire): array {
return [
'id' => $livewire->getOwnerRecord()->id,
'url' => $livewire->getOwnerRecord()->url
];
}),

->default(function (RelationManager $livewire): array {
return [
'id' => $livewire->getOwnerRecord()->id,
'url' => $livewire->getOwnerRecord()->url
];
}),
You can also turn in into an array for multiple values Np!