0

I'm making an application, where I've added this to my view-template, to show all the notifications / status-messaged / alerts:

@if ( ! empty( session('notifications') ) )
  @foreach( session('notifications') as $notification )
    <div class="alert alert-{{ $notification['notification_type'] }}" role="alert">
      <strong>{{ $notification['notification_title'] }}</strong> - {{ $notification['notification_message'] }}
    </div>
  @endforeach
@endif

I'm looking for a way, where I can simply pass a notification to a Collection stored in session('notifications'), anywhere in my controllers.

But whenever a page is loaded, that session-variable starts out empty. So I would need to both:

  • Clear all notifications upon every page load (using Session::forget('notifications') I assume, ref).
  • Instantiate an empty Collection in the session, so I don't need to check-if-empty-and-if-not-then-add-notification-and-if-it-is-empty-then-make-an-empty-collection-and-then-add-the-notification.

Where does code like this belong? I'm fairly new to Laravel, coming from WordPress, where I just would have added an action to init in functions.php. But where is the equivalent in Laravel?

And is this the proper way of controlling notifications in Laravel? I'm calling it notifications, in lack of better word. Perhaps, 'Alert' or 'Status'? Because I can see that a notification is something related, but still something else.

Zeth
  • 2,273
  • 4
  • 43
  • 91

2 Answers2

1

I think that you are looking for this : https://laravel.com/docs/5.7/session#flash-data

Right ?

Christophe
  • 389
  • 1
  • 6
  • 23
  • That is exacly it! However... What if I wanted to two flash-messages with the key 'status'? If I just set the `$request->session()->flash('status', 'Task was successful!');` in one place and `$request->session()->flash('status', 'ANOTHER MESSAGE');` later in the code, then the first flash-message will be overwritten right? Can I make it so that doesn't happen? – Zeth Nov 24 '18 at 03:55
  • This here is one way to solve it ( https://ashleyhindle.com/multiple-laravel-bootstrap-flash-messages-with-different-types/ ), - but it's not safe, since `str_random(4)` could generate two equal strings. – Zeth Nov 24 '18 at 03:58
  • 1
    When you display a flash message, you just display "the key", so you can set a lot of different message with some "different values", it depend on you ^^ But i think that using the "same key" for many message at the "same time", it's a little bit weird no ? – Christophe Nov 24 '18 at 17:34
  • Aaaaahhhh... I just got how the 'key' is supposed to work now. That the key is simply there to tell you, where the message comes from. And as shown on the Ashley Hindle-link, posted earlier, the second parameter for `flash()` can be an array with the message, the title, the color of the box and all that. Awesome. Thanks a bunch! – Zeth Nov 24 '18 at 23:08
0

To others who may need it, then here is a function for adding several flash-messages.

helper.php

Put this somewhere that is accesible globally (this post describes a way to make such a place):

function add_flash_message( array $notification){
  session()->flash( 'any_notifications', true );
  if(
    empty( session( 'notification_collection' ) )
  ){
    // If notification_collection is either not set or not a collection
    $new_collection = new \Illuminate\Support\Collection();
    $new_collection->push([
      'notification_title' => $notification['title'],
      'notification_message' => $notification['message'],
      'notification_type' => $notification['type'],
    ]);
    session()->flash( 'notification_collection', $new_collection );
  } else {
    // Add to the notification-collection
    $notification_collection = \Session::get( 'notification_collection' );
    $notification_collection->push( [
      'notification_title' => $notification['title'],
      'notification_message' => $notification['message'],
      'notification_type' => $notification['type'],
    ]);
    session()->flash( 'notification_collection', $notification_collection );
  }
}

What is does it, that it checks if there is a flash-message already. And if there is, then it'll add the new one; and if there isn't then it'll create a new Collection and add it there.

In the workflow

add_flash_message( [
  'title' => 'The file does not exist',
  'message' => 'The chosen file/path does not seem to exist.',
  'type' => 'danger'
] );

In the view/blade-file

@if( !empty( Session( 'any_notifications' ) ) )
  @foreach (Session('notification_collection') as $notification)
    <div class="alert alert-{{ $notification['notification_type'] }}" role="alert">
      <strong>{{ $notification['notification_title'] }}</strong> - {{ $notification['notification_message'] }}
      <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    </div>
  @endforeach
@endif
Zeth
  • 2,273
  • 4
  • 43
  • 91