public function likeNote($id) { $userId = Auth::user()->id; if (!NoteLike::exists($userId, $id)) { $note = Note::findOrFail($id); $noteLike = new NoteLike(); $noteLike->note_id = $id; $noteLike->user_id = $userId; $noteLike->save(); $note->updateLikes(); return $note->likes; } return -1; }
/** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $note = Note::findOrFail($id); $user = User::findOrFail($note->creator); $liked = Auth::check() && NoteLike::exists(Auth::user()->id, $note->id); $note->incHits(); return view('notes.show', compact('note', 'user', 'liked')); }
public function updateLikes() { $this->likes = NoteLike::where('note_id', $this->id)->count(); $this->save(); }
public static function exists($userId, $noteId) { return NoteLike::where('user_id', $userId)->where('note_id', $noteId)->count() > 0; }