Home > other >  How to read a parameter coming from the with method to the view in laravel
How to read a parameter coming from the with method to the view in laravel

Time:10-11

I have this route in web.php:

Route::get('student/evaluation/{evaluation}', [EvaluationController::class, 'getEvaluationQuestions'])->middleware('auth')->name('student.questionsevaluation');

And in my controller I have this condition, where $questionWasCompleted is boolean

if($questionWasCompleted){

return redirect()->route('student.questionsevaluation', $evaluation)
->with('message', 'Question answered.')
->with('question', $questionWasCompleted);
            
}

How can I get the value of $questionWasAnswered to know if it's true or false in the view file?

I tried with {{$questionWasCompleted}} in view file but it not works.

CodePudding user response:

first of all check if your view file is in blade format (example: view.blade.php) then try {{ $question }}

CodePudding user response:

with method of redirect() obj saves the key in session of requested user so in your blade file you can access it like this :

{{  session('question') }}

Also notice that first argument of with() method ( for example 'message') is the key and the second argument is ( for example 'Question answered.') is your value , for accessing the value you should use the key in your blade file .

CodePudding user response:

IF(the question was completed) this is a loop cause your end, as I think, I believe you need to change the last part and maybe use another var, OR MAYBE, use there $evaluation in the end, also you are using a return with a route, I think this is not possible, it must be in separated lanes, but I don't know well, so maybe that's good

CodePudding user response:

You are not showing what data you are sending to the view file. But typically a Route should send you to a Controller action, and inside the controller action (ex. index) you return the view you want to show and pass the data. Or maybe redirect to another route. Anyway in this case to be able to see the data to the view you should place code below inside the Controller action:

return view('view_file_name', ['message'=> 'Question answered.', 'question' => $questionWasCompleted]) 

and then you should be able to see the $message and $question values inside the view file using {{}} like: {{ $message }}. And the value of the $questionWasCompleted should be available using {{ $question }}

  • Related