/**
  * Display a list of messages between the logged in user and user $id.
  *
  * @param  int  $id
  * @return Response
  */
 public function show($id)
 {
     $userr = loadUser($id);
     if (empty($userr)) {
         flash()->error('That user does not exist');
         return redirect('/messages');
     }
     if ($userr[0]->id == \Auth::id()) {
         return $this->index();
     }
     if (!empty(loadConversation($id))) {
         $conversationId = loadConversation($id)[0]->conversationId;
         //Load all messages with in conversation $id
         //dd($conversationId);
         $messages = loadAllMessages($conversationId);
         //dd($messages);
         //Then get user with $id to show him
         $user = $userr[0];
         updateMessagesToSeen($conversationId);
         //Then get all conversations for the logged in user to show those in the sidebar
         $conversations = loadLastNConversationsWithMessage(999);
         //Find the last message that has been read by user $id
         $lastRead = "";
         if (!empty(loadLastReadMessage($id))) {
             $lastRead = loadLastReadMessage($id)[0]->message;
         }
         //Add a Carbon time object to each message
         foreach ($messages as &$message) {
             $carbon = Carbon::createFromFormat('Y-n-j G:i:s', $message->date);
             $message = (object) array_merge((array) $message, array('carbon' => $carbon));
             //Give seen status only to the last seen message, not all of them
             if ($message->message == $lastRead) {
                 $message = (object) array_merge((array) $message, array('seen' => 1));
             } else {
                 $message = (object) array_merge((array) $message, array('seen' => 0));
             }
         }
         //Add a Carbon time object to each $conversation
         foreach ($conversations as &$conversation) {
             $carbon = Carbon::createFromFormat('Y-n-j G:i:s', $conversation->date);
             $conversation = (object) array_merge((array) $conversation, array('carbon' => $carbon));
         }
         return view('pages.messages', compact('messages', 'user', 'conversations'));
     } else {
         //create a new conversation between the logged in user and $id
         $toId2 = loadUser($id)[0]->id;
         storeConversation($toId2);
         return $this->show($id);
     }
 }
 public function compose($view)
 {
     $last5notifications = loadLastNNotifications(5);
     $last5notifications = NotificationsController::buildNotifications($last5notifications);
     $last5conversations = loadLastNConversationsWithMessage(5);
     $unreadNotifications = !empty(loadUnreadNotifications());
     $unreadMessages = !empty(loadUnreadMessages());
     $noti = array();
     for ($x = 0; $x < sizeof($last5notifications); $x++) {
         $notification = $last5notifications[$x];
         array_push($noti, $notification->message, $notification->is_read);
     }
     $conv = array();
     for ($x = 0; $x < sizeof($last5conversations); $x++) {
         $conversation = $last5conversations[$x];
         //Add a Carbon time object to each $conversation
         $carbon = Carbon::createFromFormat('Y-n-j G:i:s', $conversation->date);
         $conversation = (object) array_merge((array) $conversation, array('carbon' => $carbon));
         array_push($conv, $conversation->username, $conversation->image, $conversation->message, $conversation->is_read, $conversation->author, $conversation->carbon);
     }
     $view->with(array('last5notifications' => $noti, 'last5conversations' => $conv, 'unreadNotification' => $unreadNotifications, 'unreadMessage' => $unreadMessages));
 }