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 store(Request $request, $id) { // // $conversation = Conversation::find($id); $conversation->touch(); $input = Request::all(); $input['published_at'] = Carbon::now(); $input['author_id'] = Auth::id(); $input['conversation_id'] = $id; Message::create($input); $to_users = ConversationUser::where('conversation_id', $id)->where('user_id', '!=', Auth::id())->get(['user_id']); foreach ($to_users as $_user) { $user = User::find($_user->user_id); $user->newNotification()->withType('message')->withSender(Auth::id())->withSubject($conversation->title)->withBody($input['body'])->regarding($conversation)->deliver(); } return redirect('/messages/' . $id); }