1

I'm trying to flash error messages from my controller back to my view. I tried this with:

\Route::group(['middleware' => 'web'], function ()       
    flash('Error message'); 
    return Redirect::back();
});

And tried showing it my view with:

@include('flash::message')

However this just seems not to show the message. I've been looking over the web for some good 2 to 3 hours now and I am at a loss right now.

If this is a duplication of another question somewhere on stackoverflow, then sorry!

Rick J
  • 166
  • 1
  • 15
  • 1
    No need to search for 2-3 hours when there's documentation available: https://laravel.com/docs/5.6/session#flash-data – brombeer Mar 20 '18 at 11:41
  • 1
    @kerbholz Documentation was part of that, Laravel docs kind of confuse me sometimes. So I tried using the Laracasts/flash, but their documentation is even foggier. – Rick J Mar 20 '18 at 12:27
  • 1
    Thanks kerbholz, luxshan and Rahul tathod for answering my problem. All your solutions have been really helpfull and all worked! – Rick J Mar 20 '18 at 14:08

3 Answers3

3

To use session flash in Laravel:

web.php

Route::get('/',
    function () {
        Session::flash('error', 'test');
        return view('welcome');
    });

In your .blade view file you can access the message using

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

You could replace 'error' with any type of message ('success', 'warning', 'yourOwnMessageIdentifier etc) you'd want to flash.

brombeer
  • 8,716
  • 5
  • 21
  • 27
  • Can you clarify your solution a bit more? I don't really understand it. How do I replace the 'test' and 'error' / Message identifier? I used the \Route::group(['middleware' => 'web'], function () { // do stuff}); in my controller since some other similar problems on the internet said there was something wrong with middleware grouping – Rick J Mar 20 '18 at 12:15
  • `'error'` in `Session::flash('error', 'test');` and `{{ session('error') }}` is just an identifier I use to identify the type of flash I want to output. You could also `Session::flash('success', 'Action was successful');` and then `{{ session('success') }}` in your `.blade`. Does this help? – brombeer Mar 20 '18 at 12:26
  • Yeah it does, however, this must be in the web.php? since my errors come from the 'MailController' which I use to send mail and handle my captcha verification. When it succeeds or fails it should redirect back to the page the request came from and then alert the user if the request was succesfull, or verification failed. – Rick J Mar 20 '18 at 12:29
  • Nope, `Session::flash()` can be used in your controller action too – brombeer Mar 20 '18 at 12:32
  • 1
    Yeah it works! Thanks! I've been thinking to difficult about the problem. – Rick J Mar 20 '18 at 14:10
1

In controller

 use Session;

\Session::flash('msg', 'Error' );

in blade

{!!Session::get('msg')!!}
luxshan
  • 54
  • 6
  • This seems to work, but now I've gotten curious why the laracasts/flash did not work this way? – Rick J Mar 20 '18 at 12:23
  • i haven't tried that plugin.. did you follow their documentation correctly?? and i don't think you need an external plugin for this flash message. I think you need to put Laracasts\Flash\FlashServiceProvider::class, inside 'alias'=>[] and use the methods – luxshan Mar 20 '18 at 14:19
  • I did put the Laracasts\Flash\FlashServiceProvider::class, inside the 'alias'. The plugin didn't work. But the stock session::flash what laravel has works now, I thought to difficult about how to use it. Thanks though! – Rick J Mar 21 '18 at 08:56
  • Today i came across this answer maybe it will help you https://stackoverflow.com/a/27405933/9085381 – luxshan Mar 22 '18 at 08:48
  • I used that package before I tried to get answers on stackoverflow, it kinda broke my sessions, hence this problem on stack, but thanks anyway! – Rick J Mar 26 '18 at 09:18
1
use simply
\Session::flash('msg', 'Changes Saved.' );

@if(Session::has('msg'))
        <div class="alert alert-info">
            <a class="close" data-dismiss="alert">×</a>
            <strong>Heads Up!</strong> {!!Session::get('msg')!!}
        </div>
    @endif
Rahul Tathod
  • 348
  • 5
  • 12