public function accept(Request $request)
 {
     $friends = Friends::find($request->friends_table_id);
     $query = ['user_id' => Auth::user()->id, 'friends_id' => $friends->user_id, 'created_at' => Carbon::now()];
     Friends::insert($query);
     $friends->friends_request = 0;
     $friends->save();
     return redirect('home')->with('message', 'Friend Request Accept!!');
 }
 /**
  * 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'));
 }
 public function searchRequest(Request $request)
 {
     if ($request->ajax() && !Auth::guest()) {
         $target_id = $request->target_id;
         switch ($request->mode) {
             case 'friends':
                 $query = ['user_id' => Auth::user()->id, 'friends_id' => $target_id, 'friends_request' => 1, 'created_at' => Carbon::now()];
                 Friends::insert($query);
                 $friend = User::find($target_id);
                 return ['name' => $friend->name, 'image' => $friend->image, 'email' => $friend->email];
                 break;
             case 'room':
                 $room = Room::find($target_id);
                 $query = ['room_id' => $room->id, 'user_id' => Auth::user()->id, 'created_at' => Carbon::now()];
                 DB::table('room_user')->insert($query);
                 $room_info = '                           
                 <li>
                     <a href="/room/' . $room->id . '">
                         <div class="navigation__inner--tab-body--list--box">
                             ' . Html::image($room->image, null, ['width' => '50', 'height' => '50', 'class' => 'navigation__inner--tab-body--list--box-image z-depth-1']) . '
                             <div class="navigation__inner--tab-body--list--box-description">
                                 <ul>
                                     <li>' . $room->name . '</li>
                                     <li>Belong num</li>
                                 </ul>
                             </div>
                         </div>
                     </a>
                 </li>
                 <hr>
                 ';
                 return ['name' => $room->name, 'image' => $room->image, 'email' => 'Created At:' . $room->created_at, 'room' => $room_info];
                 break;
         }
         return $request->mode;
     }
 }