FilamentF
Filament15mo ago
Medo

Advanced Nested Relationship Form builder

I have EventStaff Model Thats HasOne EventStaffDetail using staffDetails() method
    public function staffDetails(): HasOne
    {
        return $this->hasOne(EventStaffDetail::class)
            ->withDefault();
    }


Inside EventStaffDetail Model I have positions() method thats morphToMany StaffPosition
    public function positions()
    {
        return $this->morphToMany(StaffPosition::class, 'positionable')
            ->withPivot('staff_position_id')
            ->withTimestamps();
    }


So From EventStaffResource Iam able to point to staffDetails Relation by passing relationship method to section

            Section::make()->schema([
                Wizard::make()
                    ->schema([
                        Wizard\Step::make(__('Information About you'))
                            ->schema([
                                Section::make(__('Positions'))
                                    ->description(__('Which position(s) most interests you?'))
                                    ->schema([
                                        Repeater::make('positions')
                                            ->relationship('positions')
                                            ->schema([
                                                Select::make('staff_position_id')
                                                ->label('Position')
                                                    ->options(StaffPosition::all()->pluck('title', 'id')->toArray())
                                                    ->searchable()
                                                    ->required(),
                                            ]),
                                    ])
                                    ->columnSpan(1),
                            ])
                    ])
                    ->contained(false)
                    ->skippable(),
            ])->relationship('staffDetails')
image.png
Was this page helpful?