简体   繁体   中英

Laravel - how to update my value in variable and put into session laravel?

I have variable getscore in my controller function ?

 public   function questionquiz5(Request $request){


           static $getscore=0;

     $getidvalue = Input::get('getid');
     $getanswervalue = Input::get('getanswer');

     $dbscore = \DB::table('5question')->select('question_id','correct_answer','question_marks')->
     where('question_id','=',$getidvalue)->get();

   foreach ($dbscore as $value) {

       if ( $getanswervalue==($value->correct_answer) ){

                       $this->getscore  =  $this->getscore + $value->question_marks;           

          }elseif($getanswervalue == null){

                       $emptyvalue = -1;
                       $this->getscore += $emptyvalue;

          }else{

                       $novalue=0;
                       $this->getscore += $novalue;               
          }

    }
          echo "$this->getscore";


       Session::push('getscoresession',$this->getscore);


    $getsession = [  'qid' => $getidvalue,  'answer' => $getanswervalue];

       Session::push('answer', $getsession);

    return response()->json(['qid'=>$getidvalue,'answer'=>$getanswervalue]);       

}

i want to increase my value of getscore my applying foreach loop but not working ....as it always resets to zero i think? how to make it happen so when i run loop so my getscore value added and stored in session ?

Putting variables to session

session('name', 'value');

Or

$request->session()->('name', 'value');

Retrieve session

session('name');

Add to variable in foreach loop

$name = session('name');

foreach($things as $thing){
    $name += $thing
}

Update session value AFTER foreach

$request->session()->put('name', 'value')

Or

session('name', 'value');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM