8

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?

ST80
  • 3,565
  • 16
  • 64
  • 124

6 Answers6

8

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.

Tim Ogilvy
  • 1,923
  • 1
  • 24
  • 36
  • @ST80 are you using Laravel 5.8? The docs would be wrong if not! – Tim Ogilvy Jun 04 '19 at 13:12
  • @ST80 can you tell us a bit more about what is not working? It's hard to help you if you just tell us it's failed, and not how or why or what behaviour you're experiencing. – Tim Ogilvy Jun 04 '19 at 13:23
  • 1
    hmm, If I hit the network tab, the redirected route is listed and when I click on the preview tab, there is a raw version of the page -> with the message .... – ST80 Jun 04 '19 at 13:34
  • Are you submitting the update via a form submit, or an javascript call of some sort? If it's a whole-page based form-post, the session flash will occur on the page. If it's asynchronous the flash gets wasted on the invisible call. – Tim Ogilvy Jun 04 '19 at 13:37
  • 1
    Yes, the form gets submitted with javascript - ok, then thats the issue :-s – ST80 Jun 04 '19 at 14:00
  • Potentially simply using javascript to reload the page might fix this... try a simple `location.reload()` for now? – Tim Ogilvy Jun 04 '19 at 14:01
  • I've updated my answer, since we've kinda solved the problem here I think... hope you can accept it as the answer again? :D – Tim Ogilvy Jun 04 '19 at 14:22
  • 1
    Im using laravel 5.8! My validation errors were inside the session!! Different from the docs lol – Ricardinho Feb 12 '20 at 06:35
1

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;
1

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
bravohex
  • 966
  • 13
  • 21
0

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

    }
}
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
0
@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

Emeka Okafor
  • 427
  • 4
  • 10
0

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

HashtagForgotName
  • 651
  • 1
  • 7
  • 23
  • have you tried `return redirect()->route('contact.create')->withMessage('Thanks for subscribing!');` and then access them in the blade with `@if(session('message'))` – HashtagForgotName Jun 04 '19 at 12:50
  • Yes, but with not luck. If I hit the network tab, I get the redirected route and on the preview there is a raw version of the page -> with the message .... – ST80 Jun 04 '19 at 12:58
  • have you also tried `return back()` also can you say which version of laravel your using and does a `dd()` or `die()` end up on that page? – HashtagForgotName Jun 04 '19 at 13:04