PostBookmark.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class PostBookmark extends Model
  5. {
  6. protected $table = 'tb_post_bookmark';
  7. protected $primaryKey = 'id';
  8. public $keyType = 'int';
  9. public $incrementing = true;
  10. public $timestamps = true;
  11. const CREATED_AT = 'created_at';
  12. const UPDATED_AT = null;
  13. protected $guarded = [];
  14. public function board()
  15. {
  16. return $this->belongsTo(Board::class)->withDefault();
  17. }
  18. public function post()
  19. {
  20. return $this->belongsTo(Post::class)->withDefault();
  21. }
  22. public function user()
  23. {
  24. return $this->belongsTo(User::class)->withDefault();
  25. }
  26. /**
  27. * 게시글 즐겨찾기 등록
  28. */
  29. public function register(array $params): int
  30. {
  31. return $this->insertGetId($params);
  32. }
  33. /**
  34. * 게시글 즐겨찾기 삭제
  35. */
  36. public function remove(Post $post, int $userID): bool
  37. {
  38. return $this->where([
  39. ['board_id', $post->board_id],
  40. ['post_id', $post->id],
  41. ['user_id', $userID]
  42. ])->delete();
  43. }
  44. /**
  45. * 게시글 즐겨찾기 되어있는지 확인
  46. */
  47. public function isAlready(Post $post, int $userID): bool
  48. {
  49. return $this->where([
  50. ['board_id', $post->board_id],
  51. ['post_id', $post->id],
  52. ['user_id', $userID]
  53. ])->exists();
  54. }
  55. /**
  56. * 게시글 등록된 즐겨찾기 수
  57. */
  58. public function rows(Post $post): int
  59. {
  60. return $this->where([
  61. ['board_id', $post->board_id],
  62. ['post_id', $post->id]
  63. ])->count();
  64. }
  65. }