只获取从复选框返回的真值 Laravel livewire
Getting just true values returning from the checkbox Laravel livewire
请帮忙
我的数据库中有 table 个问题,我有两个属性 options[] 和 correct_answers[]
所以,我想只存储正确的答案,而不是全部存储在数据库中
因为如果我选中一个选项然后取消选中,它也会在数据库中存储错误答案
picture in database
这是我的代码:
<div>
@foreach($options as $index => $option)
<div class="flex items-center mb-2" wire:key="{{$index}}-option">
<input wire:model="answers.{{ $option }}" name="answers.{{ $option }}" id="{{ 'answers'.rand() }}"
value="answers.{{ $index }}" type="checkbox">
<input type="text" wire:model="options.{{ $index }}"
class="w-full px-3 py-2 border rounded bg-white flex-1 mr-2"
placeholder="Option ({{ $this->keys($index) }})">
</div>
@error('options') <span style="color:red"class="error">{{ $message }}</span> @enderror
@endforeach
</div>
<div wire:key="submit-button">
<button class="btn btn-success btn-lg">Create</button>
</div>
public function create()
{
$this->validate([
'question' => ['required', 'string', 'min:4', 'max:190'],
'options' => ['required', 'array'],
'options.*' => ['required', 'string'],
'answers' => 'required' ,
'type'=>'required',
'titre'=>['required', 'string', 'min:4', 'max:190'],
]);
$this->order=\App\Questions::where('exam_id',$this->exam->id)->count();
$question=new Questions();
$question->exam_id = $this->exam->id;
$question->title = $this->titre;
$question->question =$this->question;
$question->number_of_options = count($this->options);
$question->order = $this->order;
$question->response_type = $this->type;
$question->options =json_encode($this->options, JSON_UNESCAPED_UNICODE);
$question->correct_answers =json_encode($this->answers, JSON_UNESCAPED_UNICODE);
$question->save();
$this->resetQuestion();
$this->mount($this->exam);
session()->flash('message', 'Ajout effectué avec succés.');
$this->emit('closeModal');
}
可以使用 array_filter($this->answers)
删除错误答案
尝试这样的事情...
public function updatedAnswers()
{
$this->answers = array_filter($this->answers);
}
查看 Livewire 的生命周期挂钩
请帮忙 我的数据库中有 table 个问题,我有两个属性 options[] 和 correct_answers[] 所以,我想只存储正确的答案,而不是全部存储在数据库中 因为如果我选中一个选项然后取消选中,它也会在数据库中存储错误答案 picture in database
这是我的代码:
<div>
@foreach($options as $index => $option)
<div class="flex items-center mb-2" wire:key="{{$index}}-option">
<input wire:model="answers.{{ $option }}" name="answers.{{ $option }}" id="{{ 'answers'.rand() }}"
value="answers.{{ $index }}" type="checkbox">
<input type="text" wire:model="options.{{ $index }}"
class="w-full px-3 py-2 border rounded bg-white flex-1 mr-2"
placeholder="Option ({{ $this->keys($index) }})">
</div>
@error('options') <span style="color:red"class="error">{{ $message }}</span> @enderror
@endforeach
</div>
<div wire:key="submit-button">
<button class="btn btn-success btn-lg">Create</button>
</div>
public function create()
{
$this->validate([
'question' => ['required', 'string', 'min:4', 'max:190'],
'options' => ['required', 'array'],
'options.*' => ['required', 'string'],
'answers' => 'required' ,
'type'=>'required',
'titre'=>['required', 'string', 'min:4', 'max:190'],
]);
$this->order=\App\Questions::where('exam_id',$this->exam->id)->count();
$question=new Questions();
$question->exam_id = $this->exam->id;
$question->title = $this->titre;
$question->question =$this->question;
$question->number_of_options = count($this->options);
$question->order = $this->order;
$question->response_type = $this->type;
$question->options =json_encode($this->options, JSON_UNESCAPED_UNICODE);
$question->correct_answers =json_encode($this->answers, JSON_UNESCAPED_UNICODE);
$question->save();
$this->resetQuestion();
$this->mount($this->exam);
session()->flash('message', 'Ajout effectué avec succés.');
$this->emit('closeModal');
}
可以使用 array_filter($this->answers)
尝试这样的事情...
public function updatedAnswers()
{
$this->answers = array_filter($this->answers);
}
查看 Livewire 的生命周期挂钩