Пример #1
0
 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;
 }
Пример #2
0
 /**
  * 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'));
 }
Пример #3
0
 public function updateLikes()
 {
     $this->likes = NoteLike::where('note_id', $this->id)->count();
     $this->save();
 }
Пример #4
0
 public static function exists($userId, $noteId)
 {
     return NoteLike::where('user_id', $userId)->where('note_id', $noteId)->count() > 0;
 }