Search code examples
laravellaravel-livewire

Livewire binding model radio button error


i'm trying to build a polls system using livewire but i got some errors

here is my blade

@foreach ($questions as $index => $question)
    <div class="row">
        <div class="box-header with-border">
            <i class="fa fa-question-circle text-black fs-20 me-10"></i>
            <h4 class="box-title">{{ $question->title }}</h4>
        </div>
        <div class="box-body">
            <div class="demo-radio-button">
                @foreach ($answers as $key => $answer)
                    <input wire:model="question{{ $question->id }}" type="radio"
                        id="answer{{ $question->id }}{{ $key }}"
                        class="radio-col-primary" value="{{ $key }}">
                    <label
                        for="answer{{ $question->id }}{{ $key }}">{{ $answer }}</label>
                @endforeach
                @error('question')
                    <div class="text-danger text-bold">{{$message}}</div>
                @enderror
            </div>
        </div>
    </div>
@endforeach

my Livewire class

public $question = [];

public function render() {
    $category = PollCategory::findOrFail($this->category->id);
    $subCategories = PollSubCategory::where('poll_category_id', $category->id)->get();
    $answers = PollAnswer::all();
    return view('livewire.polls', compact('category', 'subCategories', 'answers'));
}

The Error is

Property [$question41] not found on component: [polls]

Any help please ?


Solution

  • you did it wrong way,

    solution:

    <input wire:model="question.{{ $question->id }}"
    

    $question{{ $question->id }} equal $question41

    $question.{{ $question->id }} equal to $question[41]

    that's why u receive error Property [$question41] not found on component