Livewire test, set array value

What seems to be the problem:
I have an array of models in a public property (called answers) in my Livewire component. I am writing tests for the component but I don’t know how to set the array values. I have tried using dot notation but it throws an exception.
Livewire\Exceptions\CannotBindDataToEloquentModelException : Cannot bind Eloquent model using [wire:model="answers.0.title"].

Steps to Reproduce:

Livewire::test(MyComponent::class)
            ->set('answers.0.title', 'blue')
            ->call('submit')
            ->assertHasNoErrors()

Are you using the latest version of Livewire:
v1.0.9

Do you have any screenshots or code examples:
Code example above.

I have managed to resolve this now by updating the assignment in the mount() method so it converts the collection of models to an array.

from

$this->answers = $question->answers;

To

$this->answers = $question->answers()->exists() ? $question->answers->toArray() : [];

1 Like