/** * Bootstrap the application services. * * @return void */ public function boot() { view()->composer('partials.navigation', function ($view) { if (Auth::guest()) { //とりあえず $rooms = Room::take(10)->get(); $friends = User::all(); $view->with(['rooms' => $rooms, 'friends' => $friends]); } else { $user_id = Auth::user()->id; $rooms = User::find($user_id)->room; $friends_id_lists = User::find($user_id)->friends->lists('friends_id'); $friends = User::whereIn('id', $friends_id_lists)->get(); $friends_request = Friends::where('friends_id', $user_id)->where('friends_request', 1)->get(); //$friends_request = Friends::whereIn('user_id' , $friends_request_lists)->get(); $view->with(['rooms' => $rooms, 'friends' => $friends, 'friends_request' => $friends_request]); } }); }
public function personalChat(Request $request) { $this->user_id = Auth::user()->id; $this->friends_id = $request->friend_id; $friend = User::find($this->friends_id); $bool = Friends::where('user_id', '=', $this->user_id)->where('friends_id', '=', $this->friends_id)->first(); Session::forget('message'); //悪意のあるユーザをメッセージ付きでhomeにリダイレクト if (empty($bool)) { return redirect('home')->with('message', 'You must become correcting a user.'); } if ($request->ajax()) { switch ($request->mode) { case 'commit': $text = $request->text; $query = ['to_user_id' => $this->friends_id, 'from_user_id' => $this->user_id, 'text' => $text, 'created_at' => Carbon::now()]; $latest_chat_id = PersonalChat::create($query)->id; //$latest_chat_id = DB::getPdo()->lastInsertId(); //相手のレコードの書き換え これで更新されているかを判定する Friends::where('user_id', $this->friends_id)->where('friends_id', $this->user_id)->update(['latest_receive_chat_id' => $latest_chat_id]); return ['self-message' => $this->selfTextHtmlBuilder(Auth::user()->image, $text), 'latest-chat-id' => $latest_chat_id]; break; case 'receive': $latest_receive_chat_id = Friends::where('user_id', $this->user_id)->where('friends_id', $this->friends_id)->pluck('latest_receive_chat_id'); if ($latest_receive_chat_id != $request->latest_chat_id) { $chats = PersonalChat::where('to_user_id', '=', $this->user_id)->where('from_user_id', '=', $this->friends_id)->where('id', '>=', $latest_receive_chat_id)->get(); $data = $this->partnerTextHtmlBuilder($friend->image, $chats); return ['message' => $data, 'latest-chat-id' => $latest_receive_chat_id]; } else { return ['message' => 'false', 'latest-chat-id' => $latest_receive_chat_id]; } break; } } $chats = PersonalChat::where('to_user_id', '=', $this->user_id)->where('from_user_id', '=', $this->friends_id)->orWhere(function ($query) { $query->where('from_user_id', '=', $this->user_id)->where('to_user_id', '=', $this->friends_id); })->orderBy('created_at')->get(); $latest_chat_id = User::find($this->user_id)->friends()->pluck('latest_receive_chat_id'); return view('chat.personal', compact('chats', 'friend', 'latest_chat_id')); }