Laravel 5.1 Redirect with variable

2019-05-25 21:33发布

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.

5条回答
相关推荐>>
2楼-- · 2019-05-25 21:56

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
查看更多
虎瘦雄心在
3楼-- · 2019-05-25 21:59

Try using the session to display it,

<input id="PrId" type="text" value="{{ isset(session($state)) ? $session($state) : '' }}">
查看更多
来,给爷笑一个
4楼-- · 2019-05-25 22:01

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.

查看更多
走好不送
5楼-- · 2019-05-25 22:04

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.

查看更多
Explosion°爆炸
6楼-- · 2019-05-25 22:08

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

查看更多
登录 后发表回答