wire-elements / modal

Livewire component that provides you with a modal that supports multiple child modals while maintaining state.
MIT License
1.09k stars 129 forks source link

Component with public property with Collection type #384

Closed lukasleitsch closed 7 months ago

lukasleitsch commented 8 months ago

A Modal Component with a Collection as public property can not be created and fails with Method Illuminate\Support\Collection::resolveRouteBinding does not exist. in https://github.com/wire-elements/modal/blob/0f64fc4c4be49a343a6e025d81629113d5ccdc93/src/Modal.php#L97

class AddNewParticipants extends ModalComponent
{
  public Collection $participants;

  public function mount(array $participants = [])
  {
    $this->participants = User::findMany($participants);
  }
}
<button
  wire:click="$dispatch('openModal', { component: 'add-new-participants', arguments: { participants: [2, 4, 5] } })"
>
  @lang('Add')
</x-button>

The Modal tries to resolve the parameter participants and finds the parameter as public property with the type Collection. It seems that this type is not handled yet.

In this case, I would like to care of resolving the parameters in the mount method and skip the resolving of the Modal Component.

PhiloNL commented 7 months ago

I think this is because the argument matches the public property. Try using IDs and computed properties instead, for example:

class AddNewParticipants extends ModalComponent
{
  public $participantsIds;

  #[Computed]
  public function participants()
  {
    return User::findMany($participantsIds);
  }
}
<button
  wire:click="$dispatch('openModal', { component: 'add-new-participants', arguments: { participantsIds: [2, 4, 5] } })"
>
  @lang('Add')
</x-button>