PostLike.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Models\DTO\SearchData;
  5. class PostLike extends Model
  6. {
  7. protected $table = 'tb_post_like';
  8. protected $primaryKey = 'id';
  9. public $keyType = 'int';
  10. public $incrementing = true;
  11. public $timestamps = true;
  12. const CREATED_AT = 'created_at';
  13. const UPDATED_AT = null;
  14. protected $guarded = [];
  15. public function board()
  16. {
  17. return $this->belongsTo(Board::class)->withDefault();
  18. }
  19. public function post()
  20. {
  21. return $this->belongsTo(Post::class)->withDefault();
  22. }
  23. public function user()
  24. {
  25. return $this->belongsTo(User::class)->withDefault();
  26. }
  27. /**
  28. * 게시판 추천/비추 삭제
  29. */
  30. public function data(SearchData $params): object
  31. {
  32. $query = $this->query();
  33. $query->select(
  34. 'tb_board.name AS boardName',
  35. 'tb_board.code',
  36. 'tb_post.subject',
  37. 'tb_post_like.*',
  38. 'user_1.sid AS sid_1',
  39. 'user_1.name AS name_1',
  40. 'user_2.sid AS sid_2',
  41. 'user_2.name AS name_2'
  42. );
  43. if($params->keyword) {
  44. switch ($params->field) {
  45. case 'tb_post_like.ip_address' :
  46. case 'user_1.name' :
  47. case 'user_1.email' :
  48. case 'user_2.name' :
  49. case 'user_2.email' :
  50. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  51. break;
  52. case 'tb_post_like.id' :
  53. case 'tb_post_like.post_id' :
  54. case 'user_1.id' :
  55. case 'user_1.sid' :
  56. case 'user_2.id' :
  57. case 'user_2.sid' :
  58. $query->where($params->field, '=', $params->keyword);
  59. break;
  60. }
  61. }
  62. if($params->startDate || $params->endDate) {
  63. if($params->startDate) {
  64. $query->where('tb_post_like.created_at', '>=', $params->startDate . ' 00:00:00');
  65. }
  66. if($params->endDate) {
  67. $query->where('tb_post_like.created_at', '<=', $params->endDate . ' 23:59:59');
  68. }
  69. }
  70. if($params->boardID) {
  71. $query->where('tb_post_like.board_id', '=', $params->boardID);
  72. }
  73. $query->join('tb_board', 'tb_board.id', '=', 'tb_post_like.board_id');
  74. $query->join('tb_post', 'tb_post.id', '=', 'tb_post_like.post_id');
  75. $query->leftJoin('users AS user_1', 'user_1.id', '=', 'tb_post_like.user_id'); // 추천인
  76. $query->leftJoin('users AS user_2', 'user_2.id', '=', 'tb_post.user_id'); // 작성자
  77. $query->orderByDesc('tb_post_like.id');
  78. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  79. $total = $this->count();
  80. $rows = $list->count();
  81. return (object)[
  82. 'total' => $total,
  83. 'rows' => $rows,
  84. 'list' => $list
  85. ];
  86. }
  87. /**
  88. * 게시글 좋아요/싫어요 정보
  89. */
  90. public function info(Post $post, int $userID): PostLike
  91. {
  92. return $this->where([
  93. ['board_id', $post->board_id],
  94. ['post_id', $post->id],
  95. ['user_id', $userID]
  96. ])->firstOrNew();
  97. }
  98. /**
  99. * 게시글 좋아요/싫어요 등록
  100. */
  101. public function register(array $params): int
  102. {
  103. return $this->insertGetId($params);
  104. }
  105. /**
  106. * 게시글 좋아요/싫어요 삭제
  107. */
  108. public function remove(Post $post, int $userID): int
  109. {
  110. return $this->where([
  111. ['board_id', $post->board_id],
  112. ['post_id', $post->id],
  113. ['user_id', $userID]
  114. ])->delete();
  115. }
  116. /**
  117. * 회원이 좋아요/싫어요 둘중 어느것 선택했는지 확인
  118. */
  119. public function isAlready(Post $post, int $userID, int $type): bool
  120. {
  121. return $this->where([
  122. ['board_id', $post->board_id],
  123. ['post_id', $post->id],
  124. ['user_id', $userID],
  125. ['type', $type]
  126. ])->exists();
  127. }
  128. }