title: Displaying Notifications Logic
body: <p>I am building a social network with Laravel framework, and I have a Notifications System.</p>
  
  <p>Basically whenever a user interacts with the website in these 5 different ways:</p>
  
  <ol>
  <li>User upvotes a Post.</li>
  <li>User comments in a Post.</li>
  <li>User upvotes a comment.</li>
  <li>User replies to a comment.</li>
  <li>User upvotes a reply.</li>
  </ol>
  
  <p>A notification is added to notifications table for that user (the OP (original poster) for instance, if a user upvotes a comment, a notification is added for the user who owns that comment, and in that notifications table I have these configuration.</p>
  
  <pre><code>$table-&gt;string('id')-&gt;primary();
          $table-&gt;string('type');
          $table-&gt;morphs('notifiable');
          $table-&gt;text('data');
          $table-&gt;timestamp('read_at')-&gt;nullable();
          $table-&gt;timestamps();
  </code></pre>
  
  <hr>
  
  <p>Hence basically I can get each logged In user's unread notifications by using command <code>Auth::user()-&gt;unreadNotifications</code> which is an array consisting of all the information of notifications.</p>
  
  <p>But here is my question. For instance 100 user upvotes one of the post of an OP, that would mean that if I loop through unread notifications array, I'll get 100 notifications, but instead I want something like this, 'last user who interacted with the post' .  'x' people have . 'interacted' with your post.'</p>
  
  <p>But I am just not able to execute it, so far I have managed to count the number of notifications user will see by using this logic:</p>
  
  <pre><code>public function get_Notification_Count()
  {
      $total_notifications = Auth::user()-&gt;unreadNotifications;
      $num_of_posts = array();
      $num_of_comments = array();
      $num_of_replies = array();
      $num_of_upvoted_comments = array();
      $num_of_upvoted_replies = array();
      // $num_of_notifications = array();
  
      foreach ($total_notifications as $notification) {
          if ($notification-&gt;type == 'App\Notifications\UserUpvotedPost') {
              array_push($num_of_posts, $notification-&gt;data['post_id']);
              $num_of_posts = array_unique($num_of_posts);
          }
          if ($notification-&gt;type == 'App\Notifications\UserCommented') {
              array_push($num_of_comments, $notification-&gt;data['post_id']);
              $num_of_comments = array_unique($num_of_comments);
          }
          if ($notification-&gt;type == 'App\Notifications\UserReplied') {
              array_push($num_of_replies, $notification-&gt;data['comment_id']);
              $num_of_replies = array_unique($num_of_replies);
          }
          if ($notification-&gt;type == 'App\Notifications\UserUpvotedComment') {
              array_push($num_of_upvoted_comments, $notification-&gt;data['comment_id']);
              $num_of_upvoted_comments = array_unique($num_of_upvoted_comments);
          }
          if ($notification-&gt;type == 'App\Notifications\UserUpvotedReply') {
              array_push($num_of_upvoted_replies, $notification-&gt;data['reply_id']);
              $num_of_upvoted_replies = array_unique($num_of_upvoted_replies);
          }
      }
  
      $num_of_notifications = count(array_merge($num_of_posts, $num_of_comments, $num_of_replies, $num_of_upvoted_comments, $num_of_upvoted_replies));
  
      return $num_of_notifications;
  }
  </code></pre>