robsontenorio / mary

Laravel Blade UI Components for Livewire 3
https://mary-ui.com
Other
804 stars 88 forks source link

Choices Str error #488

Closed erossdev closed 2 weeks ago

erossdev commented 2 weeks ago

maryUI version

^1.32

daisyUI version

^4.12.2

Livewire version

^3.5

What browsers are affected?

Chrome

What happened?

When trying to use the choices component in a livewire component I am getting an error. I have tried putting multiple different models in the :options but still the same issue.

Error Class "Str" not found

Here is the stack trace: https://flareapp.io/share/67OXVev5

PHP

public array $selected = [];

HTML/Blade

<x-choices label="Single" wire:model="selected" :options="[]" />
robsontenorio commented 2 weeks ago

It won’t work with empty options.

erossdev commented 2 weeks ago

Even adding something like :options="[[ 'id' => 1, 'name' => 'Elliott' ]]" I continue to get the same error. I have also tried other models which I know are populated.

robsontenorio commented 2 weeks ago

Have you ever tried like described on docs? :options="$myOptions"

erossdev commented 2 weeks ago

Yes. I wasn't able to get that to work either. I did some more digging and created a new laravel breeze project with livewire and class-based volt components. In one of those components I was able to get choices to work but it still doesn't work in my regular livewire component.

I removed everything from my livewire component so it is only what is below. I'm newer to PHP/Laravel so maybe I am just missing something obvious?

component:

use Livewire\Component;

class MyComp extends Component {
   public array $selected = [];
   public array $opts = [
      [ 'id' => 1, 'name' => 'First Item' ],
      [ 'id' => 2, 'name' => 'Second Item' ], 
   ];
}

view:

<div>
   <x-mary-choices label="Single" wire:model="selected" :options="$opts" single />
</div>
robsontenorio commented 2 weeks ago

About your first screenshot that code does not belongs to Choices components, but Icon component. Probably you have something different on your environment that is not loading the Str class.

About Choices, If you place the “single” attribute it means it is just one ID. So,

public ?int $selected =  null

And it works with your example for regular Livewire component.

image