I am trying to redirect in Laravel like this:
in my controller:
$state = false;
return redirect()->route('newPr')->with('errorMessageDuration', 'error', $state);
and in my view I try to get it like this:
<input id="PrId" type="text" value="{{ isset($state) ? $state : '' }}">
but somehow it does not work, it keeps being empty.
I want it to be saved permanently.
Use withErrors()
with an associative array instead, like so:
return redirect()->route('newPr')->withErrors(compact('state'));
Then you can simply use the $errors variable in your view.
From the docs:
The $errors variable is always defined and can be safely used. The
$errors variable will be an instance of Illuminate\Support\MessageBag.
For more information on working with this object, check out its
documentation.
Try using the session to display it,
<input id="PrId" type="text" value="{{ isset(session($state)) ? $session($state) : '' }}">
Use sessions.
$request->session()->flash('error', 'Some error message');
return redirect()->route('newPr');
And in the view:
@if (session('error'))
The error is {{ session('error') }}
@endif
with
method takes 2 parameters. The 1st one being the key
and 2nd one value
Or if the 1st parameter is an array its key
will be the key
and value
will be the value
.
So in your case you can do ->with('state', $state)
Or ->with(compact('state'))
with
method will flush this value to the session
so in order to retrieve the value you need to get it from session
like this session('state')
You can pass any value in this manner.
You can't send variable with redirect, but if you want try with session.
Take a look at my old answer here: Trying to pass a variable to a view, but nothing is shown in the view - laravel