Notifications in Laravel, stored in Session (how to create and clear them)

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Right ?

Method 2

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 IlluminateSupportCollection();
    $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="#" rel="nofollow noreferrer noopener" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    </div>
  @endforeach
@endif


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x