laravel 如何使用 Flash 消息返回视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42417865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:24:15  来源:igfitidea点击:

How to return view with flash message?

laravellaravel-5.2alert

提问by None

What i need is this:

我需要的是这个:

return view('protected.standardUser.includes.documents',compact('documents'))->with('successMsg','Property is updated .');

The i could use it like this:

我可以这样使用它:

  @if(Session::has('successMsg'))
    <div class="alert alert-success"> {{ Session::get('successMsg') }}</div>
  @endif

Any suggestion?

有什么建议吗?

回答by Samsquanch

It looks like you're mixing two different ways of passing data to a view, which I'm guessing is the problem. The documentationseems to indicate it's a one or the other type situation. You also seem to be mixing up view()->with()and redirect()->with(), which work differently. Try this:

看起来您正在混合两种将数据传递给视图的不同方式,我猜这是问题所在。文档似乎表明这是一种或另一种类型的情况。您似乎也混淆了view()->with()and redirect()->with(),它们的工作方式不同。尝试这个:

return view('protected.standardUser.includes.documents')->with('documents', $documents)->with('successMsg','Property is updated .');

And

@if(!empty($successMsg))
  <div class="alert alert-success"> {{ $successMsg }}</div>
@endif

回答by ARUN Madathil

I order to use session stored flash messages, need to use following code into route method.

我为了使用 session 存储的 flash 消息,需要使用以下代码进入route method.

 $request->session()->flash('successMsg','Saved succesfully!'); 

and then do

然后做

 @if(Session::has('successMsg'))
    <div class="alert alert-success"> {{ Session::get('successMsg') }}</div>
  @endif

that's it

就是这样

回答by Daniel Azamar

I'm new in Laravel and I was facing the same issue... I just tried as below::

我是 Laravel 的新手,我遇到了同样的问题......我只是尝试如下:

\Session::flash('flash','Message info');

And it works!!

它有效!!

回答by KirtJ

This works for me!

这对我有用!

return view('protected.standardUser.includes.documents',compact('documents'),
['successMsg'=>'Property is updated .']);

just remove the with()and pass it in the return view()

只需删除with()并将其传递给return view()