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);
 }
 public function hasConversation($id)
 {
     $user = Auth::user();
     $receiver = User::where('id', $id)->first();
     $userConversations = ConversationUser::where('user_id', '=', $user->id)->get(['conversation_id']);
     $receiverAndUserCommon = ConversationUser::where('user_id', $receiver->id)->whereIn('conversation_id', $userConversations)->get(['conversation_id']);
     if (!$receiverAndUserCommon->isEmpty()) {
         $otherConversations = ConversationUser::whereNotIn('user_id', array($user->id, $receiver->id))->get(['conversation_id']);
         $privateConversation = ConversationUser::whereIn('conversation_id', $receiverAndUserCommon)->whereNotIn('conversation_id', $otherConversations)->first();
         if ($privateConversation) {
             return $privateConversation;
         }
         return false;
     }
     return false;
 }
 /**
  * Create a new conversation with a given user in the database.
  *
  */
 private function createNewConversation($user_id)
 {
     $conv = new Conversation();
     $conv->save();
     $cu1 = new ConversationUser();
     $cu1->conversation_id = $conv->id;
     $cu1->user_id = \Auth::id();
     $cu1->save();
     $cu1 = new ConversationUser();
     $cu1->conversation_id = $conv->id;
     $cu1->user_id = $user_id;
     $cu1->save();
     return $conv;
 }
 /**
  * Show the form for creating a new resource.
  *
  * @return \Illuminate\Http\Response
  */
 public static function createPrivateConversation($user1, $user2)
 {
     //
     $newPrivateConversation = new Conversation();
     $newPrivateConversation->touch();
     $newPrivateConversation->save();
     $user1relation = new ConversationUser();
     $user1relation->conversation_id = $newPrivateConversation->id;
     $user1relation->user_id = $user1;
     $user1relation->touch();
     $user1relation->save();
     $user2relation = new ConversationUser();
     $user2relation->conversation_id = $newPrivateConversation->id;
     $user2relation->user_id = $user2;
     $user2relation->touch();
     $user2relation->save();
     return $newPrivateConversation->id;
 }