mercredi 3 juillet 2019

Laravel function that registered at controller always triggered, how to trigger it after I click a button

So I found before I trigger my function in controller to updating data, the data get update without I clicked the button first. I also try delay it with sleep() but it always triggered first.

I want to calculate data with the disable column value false, and after being calculate in score() function, the return value from the calculate function saved in database, and then trigger button to update disable column to being true with update() function(so the data that have been calculate not being calculate again)

my controller : function to get data to being calculate :

public function getShowResultOfQuiz($id){      
            History::create([
                'user_id'=>$user,
                'jenis_quiz_id'=>$id,
                'score'=> $score,
                'kelas' => $kelas,
                'instansi' => $instansi
            ]); 
                $categoryquiz= JenisQuiz::find($id);
                $user=Auth::user()->id;



                $score=0;
                $score=$this->score(); // function to calculate data



                $kelas = Auth::user()->kelas;
                $instansi = Auth::user()->instansi;

                    switch ($id) {
                        case '1':
                        return view('user.pages.quizresult',compact('score','categoryquiz','sarans','answers'));

                        break;

                        case '2':
                        return view('user.pages.quizanxietyresult',compact('score','categoryquiz','answers','sarans'));
                        break;

                        case '3':
                        return view('user.pages.quizdepresiresult',compact('score','categoryquiz','$answers','$sarans'));
                        break;
                    }

                } 

function to calculate the data :

public function score(){

    $user_data = Auth::user();
    $answers = $user_data->answer()->select('user_answer')->where('disable','=',false)->get();

               $a=0;
    $b = 0;
    $c = 0;
    if($answers->count()) {
        foreach ($answers as $answer) {
            switch ($answer->user_answer) {
                case 1:
                $a++;
                break;
                case 2:
                $b++;
                break;
                case 3:
                $c++;   
                break;
            }
        }

    }
    return $a+$b+$c;
}

function to update :

public function update(){
                sleep(30);
                $user_id = Auth::user()->id;
                        $user_data = User::find($user_id);
                        $answers_update = $user_data->answer()->whereDisable(false);
                        $answers_update->update(array("disable" => true));
            }

And when I'm inside view user.pages.quizresult there is an button to trigger update function

expect : call data answer with disable value false -> calculate it in score function -> save score in database-> turn disable data value = true

problem : turn data disable true -> answer data can't get by score function



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire