Home > database >  Livewire binding model radio button error
Livewire binding model radio button error

Time:01-08

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 >
        <div >
            <i ></i>
            <h4 >{{ $question->title }}</h4>
        </div>
        <div >
            <div >
                @foreach ($answers as $key => $answer)
                    <input wire:model="question{{ $question->id }}" type="radio"
                        id="answer{{ $question->id }}{{ $key }}"
                         value="{{ $key }}">
                    <label
                        for="answer{{ $question->id }}{{ $key }}">{{ $answer }}</label>
                @endforeach
                @error('question')
                    <div >{{$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 ?

CodePudding user response:

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

  •  Tags:  
  • Related