public function getConversation($id) { $_messages = Message::where(['conversation_id' => $id])->orderBy('updated_at', 'desc')->get(); $_conversation = Conversation::find($id); $messages = collect(); foreach ($_messages as $_message) { $sender = User::find($_message->author_id); $message = array(); $message['author_id'] = $_message->author_id; $message['body'] = $_message->body; $message['author_name'] = $sender->name; $message['author_surname'] = $sender->surname; $message['timestamp'] = $sender->updated_at; $messages->push($message); } $conversation_name = ""; if ($_conversation->title == "") { $conversation_name = ConversationsController::getUsersString($id); $_conversation->save(); } else { $conversation_name = $_conversation->title; } $user = User::find(Auth::id()); $unreadNotifications = $user->notifications()->unread()->get()->count(); $notifications = $user->notifications()->get(); return view('conversations.conversation')->with(['messages' => $messages, 'conversation_name' => $conversation_name, 'id' => $id, 'new_notifications_count' => $user->notifications()->unread()->not_type('message')->get()->count(), 'notifications' => $user->notifications()->not_type('message')->get(), 'new_messagesNotifications_count' => $user->notifications()->unread()->type('message')->get()->count(), 'messagesNotifications' => $user->notifications()->type('message')->get()]); }
public function createPrivate($id) { $handle = Auth::user()->hasConversation($id); if ($handle) { return redirect('/messages/' . $handle->conversation_id); } else { $privateConversationId = ConversationsController::createPrivateConversation(Auth::id(), $id); Conversation::find($privateConversationId)->title = ConversationsController::getUsersString($privateConversationId); return redirect('/messages/' . $privateConversationId); } }