6

Laravel can only show one message by type of messages (danger, warning, success, info). There is a solution for passing many warning message (for example) :

In the controller send a tab :

$messagesSuccess = [
    'test success 1',
    'test success 2'
];
$messagesInfo = [
    'test info 1',
    'test info 2'
];

$messagesWarning = [
    'test warning 1',
    'test warning 2'
];

$messagesError = [
    'test error 1',
    'test error 2'
];

$request->session ()->flash ( 'alert-info', $messagesInfo );
$request->session ()->flash ( 'alert-success', $messagesSuccess );
$request->session ()->flash ( 'alert-warning', $messagesWarning );
$request->session ()->flash ( 'alert-danger', $messagesError );

And in the php view :

            <div class="flash-message">
            <ul>
                @foreach (['danger', 'warning', 'success', 'info'] as $type_message)
                    @if(Session::has('alert-' . $type_message))
                        @foreach (Session::get('alert-' . $type_message) as $message)
                        <li><p class="alert alert-{{ $type_message }}">{{ $message }}</p></li>
                        @endforeach
                    @endif
                @endforeach
            </ul>
        </div>
        @endif <!-- end .flash-message -->

An example of the css :

.flash-message {
    border-width: 0.2 em;
    border-style: dashed;
    border-color: grey;
}

.alert {
    font-size: 1 em;
    font-weight: 800;
}

.alert-danger {
    color : #ff6c00;
}

.alert-warning {
    color : #FFD700;
}

.alert-success {
    color : green;
}

.alert-info {
    color : blue;
}
Čamo
  • 3,863
  • 13
  • 62
  • 114
ratm
  • 913
  • 1
  • 11
  • 20

1 Answers1

2

I know this is an old message, - but I just asked a similar question and got the answer here.

If I understand it correctly, then the key (the first parameter of flash()) is just used to tell where the message comes from. The second parameter kan be an array, and that can the contain the things you need to display the message correctly. I (for instance) have a flash-message like this:

session()->flash( 'file_was_not_readable', [
   'title'   => 'Error reading uploaded file'
   'message' => 'Blah di ba doo baa, di ba dee daa'
   'type'    => 'warning', 
  ]);
Zeth
  • 2,273
  • 4
  • 43
  • 91