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;
                    }),
            ]);
    }
Screenshot_2023-12-07_at_15.22.44.png
Screenshot_2023-12-07_at_15.24.31.png
Solution
yeah this:
 ->default(function (RelationManager $livewire) {
                        return $livewire->getOwnerRecord()->getAttribute('id');
                    })
Was this page helpful?