0

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 ?

Peppermintology
  • 9,343
  • 3
  • 27
  • 51
Hunter Man
  • 9
  • 1
  • 4

1 Answers1

2

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

  • i tried it but validation not working to all questions it's validate one question only $this->validate([ 'question.*' => 'required', ]); – Hunter Man Jan 07 '22 at 09:14
  • 'Asterisk symbol (*) is used to check values in the array, not the array itself.' [https://stackoverflow.com/questions/42258185/how-to-validate-array-in-laravel](array validation asterik) – Ammar Yasir Jan 07 '22 at 09:18
  • i want to make all questions are required how can i do this ? – Hunter Man Jan 07 '22 at 09:23
  • The only solution come out from my mind right now is Did your question array comes in key pair? If so ``` foreach($question as $key => $q) { $this->validate([ 'question.'.$key => ['required'] ]); } ``` – Ammar Yasir Jan 07 '22 at 09:27
  • question array comes in $question->id not $key pair .. so i need to foreach my array question ? – Hunter Man Jan 07 '22 at 09:33