Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Laravel session flash alert message not showing

Tags:

php

laravel

In my laravel-app users can check a checkbox on the contact form if they want to subscribe for a newsletter. When the checkbox is checked and the submit the form, there is a check in the controller, if the user already is subscribed and if so, a flash alert/message should appear with something like 'You are already subscribed'. Now, the check itself works, but the flash/alert message is not displaying and I don't know why.

in my view:

@if (\Session::has('success'))
    <div class="alert alert-success">
       <p>{{ \Session::get('success') }}</p>
    </div>
@endif
@if (\Session::has('failure'))
    <div class="alert alert-danger">
       <p>{{ \Session::get('failure') }}</p>
    </div>
@endif
 <div class="contact-form" id="contact">
     <form method="POST" action="{{ route('contact.store') }}">
     ...
     </form>
 </div>

and in my controller:

// when the checkbox is checked!

if (!empty(request()->newsletter)) {
    if (!Newsletter::isSubscribed(request()->email)) {
        Newsletter::subscribePending(request()->email);

        return redirect()->route('contact.create')->with('success', 'Thanks for subscribing!');

    } else {

        return redirect()->route('contact.create')->with('failure', 'You are already subscribed');

    }
}

Can someone help me out?

like image 502
ST80 Avatar asked Jun 04 '19 12:06

ST80


2 Answers

You appear to be using the session class, rather than the session helper laravel injects on the service container.

If you're using Laravel 5.8, session in blade is a helper function as per the docs here:

https://laravel.com/docs/5.8/responses#redirecting-with-flashed-session-data

An example

Route::post('user/profile', function () {
    // Update the user's profile...

    return redirect('dashboard')->with('status', 'Profile updated!');
});

So use the helper function in blade like this:

@if (session('status'))
    <div class="alert alert-success">
        {{ session('status') }}
    </div>
@endif

NB: This will only work if you are using a page submit. If it's a javascript submit you may need to refresh the page to get the alert to show up.

like image 74
Tim Ogilvy Avatar answered Nov 01 '22 10:11

Tim Ogilvy


If the above answers are not getting you the desired result you should check if your request gets through the validation.

Try to dd() the request after the validation and see if you get the results from your request.

If the problem lies with the flashing of the session this is how I like to do it.

if (!empty(request()->newsletter)) {
    if (!Newsletter::isSubscribed(request()->email)) {
        Newsletter::subscribePending(request()->email);
        Session::flash('message', 'Thanks for subscribing!');
        Session::flash('alert-class', 'alert-success');
        return redirect()->route('contact.create');

    } else {
        Session::flash('message', 'You are already subscribed!');
        Session::flash('alert-class', 'alert-danger');
        return redirect()->route('contact.create');

    }
}

Then in the view I use this code block for the message.

  @if(Session::has('message'))
      <div class="form-group row">
          <div class="col-6">
              <p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{ 
                Session::get('message') }}</p>
          </div>
      </div>
  @endif

Dont forget the reference to the session on the top of your controller!

use Session;
like image 1
Bombajofter Avatar answered Nov 01 '22 10:11

Bombajofter