Im try using checkbox in livewire 2.x, its ok when using single property. But its not worked when using nested property.
blade :
@foreach ($questions as $key => $question)
<div class="card card-primary">
<div class="card-body">
<div class="section-title mt-0">{{ $question->description }}</div>
<div class="form-group">
@foreach ($question->choices as $choice)
<div class="form-check form-check-inline col-12 col-md-5 col-lg-5">
<input
class="form-check-input"
type="checkbox"
id="checkbox-choice-{{ $choice->id }}"
value="{{ $question->id }}_{{ $choice->id }}"
wire:model.defer='answers.{{ $key }}.choices'
>
<label class="form-check-label" for="checkbox-choice-{{ $choice->id }}">{{ $choice->description }}</label>
</div>
@endforeach
</div>
</div>
</div>
@endforeach
then on component :
public $answers = [];
public $rules = [
'answers' => 'required|array|min:1',
];
public function submitAnswers()
{
/** @var array $validData */
$validData = $this->validate();
Log::info('' . json_encode($validData));
}
example result :
{"answers":[{"choices":"42_167"}]}
property choices only returns a string result even though it has selected multiple choices, how to make property choices an array?