0
votes

In am getting Undefined variable: score (View: C:\Users\Sarthak\blog\resources\views\submitquestion.blade.php) in blade view when executing :

Controller part

public function question(Request $request)
{
    static $startscore = 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)) {
            $getscore = $startscore + $value->question_marks;
        }
        elseif ($getanswervalue == null) {
            $emptyvalue = - 1;
            $getscore = $startscore + $emptyvalue;
        }
        else {
            $novalue = 0;
            $getscore = $startscore + $novalue;
        }
    }

    echo "$getscore";
    Session::push('getscoresession', $getscore);
    $getsession = ['qid' => $getidvalue, 'answer' => $getanswervalue];
    Session::push('answer', $getsession);

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

    $score = array_sum(Session::get("getscoresession"));

    // return view('submitquestion',compact('score'));

    return view('submitquestion', ['score' => $score]);
}

Blade part :

You have submitted quiz and your score is : {{ $score }}> LOGOUT
3
In the blade file, use @{{$score}}, instead of {{ $score }}Dev
it worked but my value doesn't pass from controller to view... it showsprasoon mudgal
You have submitted quiz and your score is : {{ $score }} > LOGOUTprasoon mudgal
You have used $score as your variable name when passing the data to the view. This results in blade creating a variable called $$scoreZubair Nazer Oliyat
i have to pass my session array value in variable and then passes it to blade part how i can proceed ?prasoon mudgal

3 Answers

0
votes

try this one:

return view('submitquestion', compact('score'));
0
votes

you can pass variable form controller to view using with and compact:

$request->session()->put('getscoresession', $getscore);
$getsession = ['qid' => $getidvalue, 'answer' => $getanswervalue];
$request->session()->put('answer', $getsession);

if ($request->session()->has('getscoresession')) {
     $score = array_sum($request->session()->get("getscoresession"));
}else{
      $score = 0;
 }

using with :

$score= 10;  
return view('submitquestion')->with('score',$score);

using compact :

return view('submitquestion',compact('score'));
0
votes

Try it. If you get error from your controller send $score = 10; return view('submitquestion')->with(['score' => $score]);