Livewire 绑定模型单选按钮错误
Livewire binding model radio button error
我正在尝试使用 livewire 构建投票系统,但我遇到了一些错误
这是我的 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
我的 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'));
}
错误是
Property [$question41] not found on component: [polls]
有什么帮助吗?
你做错了,
解决方案:
<input wire:model="question.{{ $question->id }}"
$question{{ $question->id }} 等于 $question41
$question.{{ $question->id }} 等于 $question[41]
这就是你收到错误的原因 Property [$question41] not found on component
我正在尝试使用 livewire 构建投票系统,但我遇到了一些错误
这是我的 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
我的 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'));
}
错误是
Property [$question41] not found on component: [polls]
有什么帮助吗?
你做错了,
解决方案:
<input wire:model="question.{{ $question->id }}"
$question{{ $question->id }} 等于 $question41
$question.{{ $question->id }} 等于 $question[41]
这就是你收到错误的原因 Property [$question41] not found on component