lundi 22 juin 2020

Getting just true values returning from the checkbox Laravel livewire

Please Help i have in my database table questions and i have two attributes options[] and correct_answers[] So, I want to store just the correct answers not all in the database because if i check an option and then i unchecked , it store in database also the false answers picture in database

this is my code :

  <div>
        @foreach($options as $index => $option)
            <div class="flex items-center mb-2" wire:key="-option">
                    <input wire:model="answers." name="answers." id="answers" 
                    value="answers." type="checkbox">
                      <input type="text" wire:model="options."
                    class="w-full px-3 py-2 border rounded bg-white flex-1 mr-2"
                    placeholder="Option ()">
            </div>
            @error('options') <span style="color:red"class="error"></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');
    }



Aucun commentaire:

Enregistrer un commentaire