F
Filament2mo ago
Yoru

Saving an unsaved id using RelationManager

Hi, I’m building a ticket booking website where each event can have multiple “ticket types.” A ticket type can either be Single or Bundle. Single tickets: regular tickets. Bundle tickets: depend on a “base ticket” (which must be a Single ticket), and when purchased, they multiply the base ticket. I’m handling this with a Relation Manager: when creating an event, I can also create its related ticket types (Single or Bundle). The problem is: - When I create a new Single ticket and a new Bundle ticket at the same time, and I set the Bundle’s base_ticket_id to the new Single, it stores it as an unsaved ID. What I’d like is to save the Single ticket first and then use its fresh ID for the Bundle. - If I delete a Single ticket, the Bundle still keeps the old base_ticket_id. Instead, I’d like the Bundle to also be deleted (or handled gracefully). Has anyone dealt with a similar case and what would be the best approach here? Thanks!
1 Reply
Yoru
YoruOP2mo ago
This is my approach and it has those problems
Select::make('base_type_id')->label('Ticket Type to be Multiplied')
->helperText('Base Type is the ticket that will be generated when user bought one')
->options(function (callable $get, $set) {
$showId = $get('../../id'); $savedOptions = TicketType::query()
->where('show_id', $showId)
->where('mode', 'single')
->pluck('name', 'id')
->toArray();

//obtain unsaved options
$unsavedOptions = collect($get('../../ticket_types'))
->filter(fn ($item) => isset($item['mode']) && $item['mode'] === 'single' && ! isset($item['id']))
->mapWithKeys(fn ($item, $key) => ["unsaved-{$key}" => $item['name']])
->toArray();

// if unsavedoptions has null values, remove them
$unsavedOptions = array_filter($unsavedOptions, fn ($value) => ! is_null($value));
return $savedOptions + $unsavedOptions;})
->preload()
->searchable()
->required(fn ($get) => $get('mode') == 'bundle')
->visible(fn ($get) => $get('mode') == 'bundle'),
Select::make('base_type_id')->label('Ticket Type to be Multiplied')
->helperText('Base Type is the ticket that will be generated when user bought one')
->options(function (callable $get, $set) {
$showId = $get('../../id'); $savedOptions = TicketType::query()
->where('show_id', $showId)
->where('mode', 'single')
->pluck('name', 'id')
->toArray();

//obtain unsaved options
$unsavedOptions = collect($get('../../ticket_types'))
->filter(fn ($item) => isset($item['mode']) && $item['mode'] === 'single' && ! isset($item['id']))
->mapWithKeys(fn ($item, $key) => ["unsaved-{$key}" => $item['name']])
->toArray();

// if unsavedoptions has null values, remove them
$unsavedOptions = array_filter($unsavedOptions, fn ($value) => ! is_null($value));
return $savedOptions + $unsavedOptions;})
->preload()
->searchable()
->required(fn ($get) => $get('mode') == 'bundle')
->visible(fn ($get) => $get('mode') == 'bundle'),

Did you find this page helpful?