public function isFavourited() { if (!Auth::check()) { return false; } return Favourite::where('user_id', Auth::user()->id)->where('snippet_id', $this->id)->count() > 0 ? true : false; }
public function postFavourite(Request $request) { if (!$request->has('snippet_id')) { return "fail"; } $validator = $this->validator($request->all()); if ($validator->fails()) { return "fail"; } $q = Favourite::where('user_id', Auth::user()->id)->where('snippet_id', $request->get('snippet_id')); $exists = $q->count() > 0 ? true : false; if ($exists) { $q->first()->delete(); return "deleted"; } else { $this->create($request->all()); return "success"; } }