Saving relationships when creating record with Repeater

I have an already working application with Filament v2, Laravel 9, PHP 8.0. There's a Model called Menu. A Menu could be available on a given week, so when you create a Menu you have to assign Offers to it, and also Meals to the Offers, which is set on the Dish Model with the price and an active toggle. So that's Menu hasMany Offer, Offer hasMany Dish, Dish belongsToMany Meal. This was already working well with 7 weekdays, but now there's a request to make it work with any amount of days, even like only weekends. Let me show you the essential parts of the code.
Forms\Components\Repeater::make('offers')
->relationship('offers')
->defaultItems(7)
->minItems(7)
->maxItems(7)
->disableItemCreation()
->disableItemDeletion()
->schema([
Forms\Components\Toggle::make('active')
...,
Forms\Components\TextInput::make('price')
->integer()
...,
Forms\Components\Grid::make(1)
->relationship('dish')
->schema([
Forms\Components\Select::make('meal_id')
->multiple()
// ->required() Not anymore
->options(Meal::all()->pluck('nameWithPrice', 'id')),
])
])
Forms\Components\Repeater::make('offers')
->relationship('offers')
->defaultItems(7)
->minItems(7)
->maxItems(7)
->disableItemCreation()
->disableItemDeletion()
->schema([
Forms\Components\Toggle::make('active')
...,
Forms\Components\TextInput::make('price')
->integer()
...,
Forms\Components\Grid::make(1)
->relationship('dish')
->schema([
Forms\Components\Select::make('meal_id')
->multiple()
// ->required() Not anymore
->options(Meal::all()->pluck('nameWithPrice', 'id')),
])
])
I know the Repeater could be used like... Add an item, then choose which day, than add another if wanted, but we want to keep these 7 items fixed, it works better UX wise. How can I tell the Repeater somehow, that it should only save the Offer when that meal_id on the Grid::relationship('dish') exists? I tried to solve this with ->saveRelationshipsUsing(), but no luck so far.
3 Replies
Benjámin
Benjámin8mo ago
up, please?
Becker Maxime
Becker Maxime8mo ago
Why not use the create() function to check your data.
Dennis Koch
Dennis Koch8mo ago
Isn't there an ->exists() validation? You might need to tweak it to respect your relation