public function postFavorite(Request $request) { if ($request->input('toggle') == 'u') { $existanceCheck = UserFavorites::where('user_id', '=', Auth::user()->id)->where('favorited_id', '=', $request->input('user'))->count(); if ($existanceCheck) { $follow = UserFavorites::where('user_id', '=', Auth::user()->id)->where('favorited_id', '=', $request->input('user'))->first(); $follow->delete(); $user = User::find($request->input('user')); $user->favorites--; $user->save(); echo 'ok'; } else { echo 'error'; } } elseif ($request->input('toggle') == 'f') { $existanceCheck = UserFavorites::where('user_id', '=', Auth::user()->id)->where('favorited_id', '=', $request->input('user'))->count(); if (!$existanceCheck) { $follow = new UserFavorites(); $follow->user_id = Auth::user()->id; $follow->favorited_id = $request->input('user'); $follow->save(); $user = User::find($request->input('user')); $user->favorites++; $user->save(); echo 'ok'; } else { echo 'error'; } } }
public function askIsFavoriting($user) { $res = UserFavorites::where('user_id', '=', Auth::user()->id)->where('favorited_id', '=', $user->id)->count(); return $res > 0; }