简体   繁体   中英

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. In my main.blade.php i wrote the following code for getting message on my page

     @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. Any solution or tutorial will be your best anticipation. Regards

You should use session::has() , the has function simply checks if it exists. To use the value in the session use 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'); You can use 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.

For more information about Laravel Sessions have a look at the documentation: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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