To achieve the desired functionality, you'll need to make a few adjustments to your Livewire component. Here's a step-by-step guide to modify your rsvpAction method to handle the Attendee lookup and display the child attendees in the second step of the wizard.
- Modify the
afterStateUpdatedcallback to set theattendeeproperty correctly. - Use a conditional to determine if the attendee is a main attendee or a child attendee and load the appropriate child attendees.
- Update the
schemafor theAttendancestep to dynamically generate checkboxes for each child attendee.
Here's how you can adjust your code:
class Home extends Component implements HasForms, HasActions
{
use InteractsWithActions;
use InteractsWithForms;
public ?Attendee $attendee = null;
public $childAttendees = [];
public function rsvpAction(): Action
{
return Action::make('rsvp')
->label('RSVP')
->slideOver()
->steps([
Step::make('Name')
->description('What is your name?')
->schema([
TextInput::make('name')
->required()
->afterStateUpdated(function ($state) {
$this->attendee = Attendee::where('name', $state)->first();
if ($this->attendee) {
// Determine if the attendee is a main attendee or a child attendee
$mainAttendee = $this->attendee->attendee_id ? $this->attendee->mainAttendee : $this->attendee;
// Load the child attendees
$this->childAttendees = $mainAttendee->childAttendees;
}
})
])
->afterValidation(function (Get $get) {
if (!$this->attendee) {
throw new Halt();
}
}),
Step::make('Attendance')
->description('Will you be attending?')
->schema([
Checkbox::make('Will you be attending?')
->required(),
// Dynamically generate checkboxes for each child attendee
...$this->getChildAttendeeSchema(),
])
])
->action(function (array $data) {
info(print_r($data, true));
});
}
// Helper method to generate the schema for child attendees
private function getChildAttendeeSchema()
{
return collect($this->childAttendees)->map(function ($childAttendee) {
return Checkbox::make("childAttendees.{$childAttendee->id}.attending")
->label("Will {$childAttendee->name} also attend?");
})->toArray();
}
public function render(): View
{
return view('livewire.home');
}
}
In this solution, we've added a new property $childAttendees to store the child attendees. We've also created a helper method getChildAttendeeSchema to dynamically generate the schema for the child attendees based on the loaded $childAttendees property.
Please note that this code assumes that you have a view file livewire.home that is properly set up to render the Livewire component. Also, ensure that your Livewire and Filament versions are up to date to avoid any compatibility issues with the provided code.