PHP Laravel session flash alert message not showing

Viewed 17742

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?

6 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.

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;

In my case, session flash just working on form submit and return redirect().

I working on Laravel 5.8

-Controller

class HomeController extends Controller
{
    public function index()
    {
        return view('welcome');
    }

    public function test(Request $request)
    {
        return redirect()->back()->with(['success' => 'Thanks for subscribing']);
    }
}

  • Route
Route::get('/home', 'HomeController@index');
Route::post('/test', 'HomeController@test');
  • View
@if ($message = session('success'))
  <div class="alert alert-success alert-block">
    <strong>{{ $message }}</strong>
  </div>
@endif

I Think you should pass value as array ['success'=>'Thanks for subscribing!', try this.

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']);

    }
}
@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

i love to do it this way, you can try it

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

then edit your controller

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']);

    }
}

this way it will return an array and not an object

You can pass the values with WithMessage instead of with so in the controller you would redirect like this return back()->withMessage('Thanks for subscribing');then in the blade you can access the message with

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

And for the error message I would personally use the Validator of laravel it self so you can access the errors like this

@if($errors->any())
<div class="form-group">
    @foreach($errors->all() as $error)
        <div class="alert alert-danger">
            <ul>
                <li> {{ $error }} </li>
            </ul>
        </div>
    @endforeach
</div>
@endif

also like mention by @Niklesh Raut you can also make it an array and access them like an @if statement in the blade

Related