How to access laravel flash data/message in ajax?

17,688

Instead of hardcoding html with the server's response like @Abbbas khan proposes, I would rather return a sub-view for flash messages and have the possibility to reuse that sub-view on any page.

1. Create your flash messages sub-view: Create partials/flash-messages.blade.php

@if( Session::has("success") )
<div class="alert alert-success alert-block" role="alert">
    <button class="close" data-dismiss="alert"></button>
    {{ Session::get("success") }}
</div>
@endif

//Bonus: you can also use this subview for your error, warning, or info messages 
@if( Session::has("error") )
<div class="alert alert-danger alert-block" role="alert">
    <button class="close" data-dismiss="alert"></button>
    {{ Session::get("error") }}
</div>
@endif

2. In your controller: Define the success message you want and return your flash-messages sub-view

use Session;
use View;

Session::flash('success', 'File has been uploaded successfully!');
return View::make('partials/flash-messages');

3. In your main view: Place a empty div at the location you'd like jquery to append your flash message

<div class="flash-message"></div>

4. In your ajax code: Pass your sub-view as a html response and attach it to the div defined above

$.ajax({
    ...
    dataType: 'html', //Optional: type of data returned from server
    success: function(data) {
        $('div.flash-message').html(data);
    },
    ...
});

5. Why I like this approach: With this server-side approach, you have your flash messages ready to be reused on any page. Your ajax code is also streamlined and you can also simply reuse it again on another page. The only thing you need to customise is the message content that you neatly define in your controller! Awesome!

Share:
17,688
Ayaz Ali Shah
Author by

Ayaz Ali Shah

I've been a software engineer since 2013. I have experience programming in PHP/MySQL, JavaScript, and I have many other coding skills. If need contact me: imphpdevelope () gmail () com

Updated on July 30, 2022

Comments

  • Ayaz Ali Shah
    Ayaz Ali Shah almost 2 years

    I'm trying to access laravel flash messages in the ajax request. I searched about it there are some solutions available, but i want to show them without reloading the page.

    What I'm Trying :

    In my controller i'm setting flash message

    Session::flash('success', 'Record has been inserted successfully'); 
    

    And

    return response()->json([
        'success' => 'ok' // for status 200
    ]);
    

    In Ajax

    success: function(data){
        if(data.success){
            // reload the current page
            window.location.href = 'http://localhost/Framework/augLaravel3/public/index.php/user/add'; 
        }
    }
    

    In View

     @if (Session::has("success"))
       {{ Session::get("success") }}
     @endif
    

    Above code is working properly, i just want to remove the reloading step, because i'm think it will be more convenient for complex or big application if i show the message through ajax (without page reload). But as i'm new to laravel so i don't know can i convert session::flash to json or not?

    Can anyone guide me about this, that its possible? and it will be correct way if we access session::flash in ajax? I would like to appreciate if someone guide me. Thank You.