How to Show a flash message in Laravel

Problem:

How to Show a flash message in Laravel?

Solution:


Display flash message in Laravel 4 to Laravel 5.5+.

1) In our controller, at the top, we have to use Session class first.
use Session;

Then Inside any method, make a flash message:
Session::flash('success', 'The Data has inserted successfully in the Database');

So now, here I've created a success variable which contains the message "The Data has inserted successfully in the Database" in any view just for one time.

And in our View File, we've displayed that by this way.
@if ( Session::has('success') )
<div class="alert alert-success">
  <strong>{{ Session::get('success') }}</strong>
</div>
@endif


That's it. Your flash message will be displayed in your page. Like :

How to Show a flash message in Laravel
I've just added simple stylesheet. Like,

<style>
         .alert{
       border-left : 5px solid blue;
               padding:10px;
      }
         .alert-success{
       background : #7CB473;
      }
</style>

Without include session class. In todays Laravel, the Session class is included to the input requests. So, we can use the session without including Session:: class. Like that,
public function store(Request $request)
    {
        //Store the Data First
        $request->session()->flash('success', 'Student has inserted successfully in the student list.!!');
    }

So using the request object we can access the session class and session flash message and all other sessions.

Note
Having any problem to include session flash message in Laravel, please comment below.



Tags:
How to Show a flash message in Laravel, Laravel Session, Laravel Flash Message, Laravel Notification, Laravel display a flash alert.
How to Show a flash message in Laravel How to Show a flash message in Laravel Reviewed by Maniruzzaman Akash on October 23, 2017 Rating: 5

9 comments:

  1. Hi,
    thanks for sharing useful information,

    Providing Laravel Web Developmentl shopping cart, Website Designing and Payment gateway integration.

    Ecommerce Website Development
    Online Business Website Development
    Ecommerce Portal Development

    For more details about my Web Development and Laravel Web Development Services.

    ReplyDelete
  2. We are a specialized Laravel Development Company, which offers the best travel Website & Application Services. We assure effective and quality work . +91-9806724185 or Contact@expresstechsoftwares.com

    ReplyDelete

Powered by Blogger.