Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with BelongsToMany repeator on edit #122

Open
sheepbild opened this issue Oct 10, 2023 · 3 comments
Open

Bug with BelongsToMany repeator on edit #122

sheepbild opened this issue Oct 10, 2023 · 3 comments

Comments

@sheepbild
Copy link

Hello,
I tried to use Repeator with BelongsToMany relationship. The classes are "Event (object in calendar)" -> "Reservation" <- "User (student)".
If I use the same code in a classical FilamentPHP Edit page, my "Repeator" function looks fine. When I click on "Save" I've this issue :

Call to a member function reservations() on null

Docs :
https://filamentphp.com/docs/3.x/forms/fields/repeater

My getFormSchema() function :

[
            View::make('forms.components.title-event'),
            DateTimePicker::make('start')
                ->label('Début')
                ->default(now())
                ->seconds(false)
                ->required(),
            DateTimePicker::make('end')
                ->label('Fin')
                ->default(now())
                ->seconds(false)
                ->required(),
            Repeater::make('reservations')
                ->relationship()
                ->schema([
                    Select::make('student_id')
                        ->label('Participant')
                        ->required()
                        ->relationship('student', 'name')
                        ->options(User::all()->sortBy('name')->pluck('name', 'id'))
                        ->searchable()
                        ->default(null),
                    Toggle::make('has_participated')
                        ->label('A participé'),
                    Toggle::make('is_paid')
                        ->label('Payé'),
                ])
                ->columns(1)
        ]

Full classes : https://github.com/sheepbild/filament-viewcolumn-bug/tree/main/belongsToMany-repeater

@tjimenez
Copy link

tjimenez commented Nov 3, 2023

it happens with MorphToSelect too.

@KonstantinosVazaios
Copy link

Hello, I tried to use Repeator with BelongsToMany relationship. The classes are "Event (object in calendar)" -> "Reservation" <- "User (student)". If I use the same code in a classical FilamentPHP Edit page, my "Repeator" function looks fine. When I click on "Save" I've this issue :

Call to a member function reservations() on null

Docs : https://filamentphp.com/docs/3.x/forms/fields/repeater

My getFormSchema() function :

[
            View::make('forms.components.title-event'),
            DateTimePicker::make('start')
                ->label('Début')
                ->default(now())
                ->seconds(false)
                ->required(),
            DateTimePicker::make('end')
                ->label('Fin')
                ->default(now())
                ->seconds(false)
                ->required(),
            Repeater::make('reservations')
                ->relationship()
                ->schema([
                    Select::make('student_id')
                        ->label('Participant')
                        ->required()
                        ->relationship('student', 'name')
                        ->options(User::all()->sortBy('name')->pluck('name', 'id'))
                        ->searchable()
                        ->default(null),
                    Toggle::make('has_participated')
                        ->label('A participé'),
                    Toggle::make('is_paid')
                        ->label('Payé'),
                ])
                ->columns(1)
        ]

Full classes : https://github.com/sheepbild/filament-viewcolumn-bug/tree/main/belongsToMany-repeater

Make sure in your Custom Widget Class to include these 2 methods

protected function getFormModel(): Model|string|null
{
return $this->event ?? Reservation::class;
}

public function resolveEventRecord(array $data): Reservation
{
return Reservation::find($data['id']);
}

@Elleand
Copy link

Elleand commented Feb 18, 2024

Hello, I tried to use Repeator with BelongsToMany relationship. The classes are "Event (object in calendar)" -> "Reservation" <- "User (student)". If I use the same code in a classical FilamentPHP Edit page, my "Repeator" function looks fine. When I click on "Save" I've this issue :
Call to a member function reservations() on null
Docs : https://filamentphp.com/docs/3.x/forms/fields/repeater
My getFormSchema() function :

[
            View::make('forms.components.title-event'),
            DateTimePicker::make('start')
                ->label('Début')
                ->default(now())
                ->seconds(false)
                ->required(),
            DateTimePicker::make('end')
                ->label('Fin')
                ->default(now())
                ->seconds(false)
                ->required(),
            Repeater::make('reservations')
                ->relationship()
                ->schema([
                    Select::make('student_id')
                        ->label('Participant')
                        ->required()
                        ->relationship('student', 'name')
                        ->options(User::all()->sortBy('name')->pluck('name', 'id'))
                        ->searchable()
                        ->default(null),
                    Toggle::make('has_participated')
                        ->label('A participé'),
                    Toggle::make('is_paid')
                        ->label('Payé'),
                ])
                ->columns(1)
        ]

Full classes : https://github.com/sheepbild/filament-viewcolumn-bug/tree/main/belongsToMany-repeater

Make sure in your Custom Widget Class to include these 2 methods

protected function getFormModel(): Model|string|null { return $this->event ?? Reservation::class; }

public function resolveEventRecord(array $data): Reservation { return Reservation::find($data['id']); }

Thank you very much. I have been trying to debug this issue for whole afternoon. !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants