简体   繁体   English

Laravel 5.3中的Flash消息

[英]Flash Message in Laravel 5.3

I am using laravel 5.3 and i want to show a message to user when user singup to the website. 我正在使用laravel 5.3,我希望在用户单独登录网站时向用户显示一条消息。 In my main.blade.php i wrote the following code for getting message on my page 在我的main.blade.php中,我编写了以下代码,用于在我的页面上获取消息

     @if(Session::has('message'))
      div class="alert">
      {{Session::has('message')}}
      </div>
      @endif

In my controller function i want to redirect user to my home page for this purpose i wrote down following code 在我的控制器功能中,我想将用户重定向到我的主页,为此我记下了下面的代码

    return Redirect::to('/')->with('message','you have singup successfully please login');

Now on my home page a div is coming with respective style but message is not coming in this div.Instead of the message "1" is coming in this div. 现在在我的主页上一个div来了各自的风格,但消息不会出现在这个div中。而不是消息“1”即将进入这个div。 Any solution or tutorial will be your best anticipation. 任何解决方案或教程都是您最好的期待。 Regards 问候

You should use session::has() , the has function simply checks if it exists. 你应该使用session::has()has函数只是检查它是否存在。 To use the value in the session use session::get() . 要在会话中使用session::get() ,请使用session::get()

This code should serve you well: 这段代码应该很好地为您服务:

<div class="alert"> {!! Session::get('message') !!} </div>

If you want the message only to be shown once, you could flash the session. 如果您希望仅显示一次消息,则可以刷新会话。

Instead of using Redirect::to('/')->with('x','y'); 而不是使用Redirect::to('/')->with('x','y'); You can use Session::flash('message', 'This message will be shown once!' ); 您可以使用Session::flash('message', 'This message will be shown once!' ); but you'll have to add that code before you do the redirect and you won't need the with() anymore. 但是你必须在进行重定向之前添加该代码,你不再需要with()了。

For more information about Laravel Sessions have a look at the documentation: https://laravel.com/docs/5.4/session 有关Laravel Sessions的更多信息,请查看文档: https ://laravel.com/docs/5.4/session

  Try this code:

   In  Controller:

    if ($x->save()) {
                    return Redirect::route('/')->with('success', 'Added Successfully!!');
                } else {
                    return Redirect::route('/')->with('fail', 'Failed');
                }

    View Page:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM