【问题标题】:Livewire binding model radio button errorLivewire 绑定模型单选按钮错误
【发布时间】:2022-01-07 09:04:41
【问题描述】:

我正在尝试使用 livewire 构建投票系统,但我遇到了一些错误

这是我的刀

@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 课程

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'));
}

错误是

在组件上找不到属性 [$question41]:[polls]

有什么帮助吗?

【问题讨论】:

    标签: laravel laravel-livewire


    【解决方案1】:

    你做错了,

    解决方案:

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

    $question{{ $question->id }} 等于 $question41

    $question.{{ $question->id }} 等于 $question[41]

    这就是为什么你会收到错误Property [$question41] not found on component

    【讨论】:

    • 我试过了,但验证不适用于所有问题它只验证一个问题 $this->validate([ 'question.*' => 'required', ]);
    • '星号 (*) 用于检查数组中的值,而不是数组本身。 [stackoverflow.com/questions/42258185/… 验证星号)
    • 我想提出所有问题我该怎么做?
    • 我现在想到的唯一解决方案是您的问题数组是否包含在密钥对中?如果是 ``` foreach($question as $key => $q) { $this->validate([ 'question.'.$key => ['required'] ]); } ```
    • 问题数组来自 $question->id 而不是 $key 对.. 所以我需要 foreach 我的数组问题?
    猜你喜欢
    • 2013-06-29
    • 2021-07-19
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2011-01-18
    • 1970-01-01
    相关资源
    最近更新 更多