PostTag.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Models\DTO\SearchData;
  6. class PostTag extends Model
  7. {
  8. protected $table = 'tb_post_tag';
  9. protected $primaryKey = 'id';
  10. public $keyType = 'int';
  11. public $incrementing = true;
  12. public $timestamps = true;
  13. const CREATED_AT = 'created_at';
  14. const UPDATED_AT = null;
  15. protected $guarded = [];
  16. public function board()
  17. {
  18. return $this->belongsTo(Board::class);
  19. }
  20. public function post()
  21. {
  22. return $this->belongsTo(Post::class);
  23. }
  24. public function user()
  25. {
  26. return $this->belongsTo(User::class);
  27. }
  28. /**
  29. * 게시글 태그 조회
  30. */
  31. public function data(SearchData $params): object
  32. {
  33. $query = $this->query();
  34. $query->from(function($q) use($params) {
  35. $q->select(
  36. 'tb_board.name AS boardName',
  37. 'tb_board.code',
  38. 'tb_post_tag.*',
  39. 'tb_post.subject',
  40. 'tb_post.user_id',
  41. 'users.sid',
  42. 'users.name'
  43. );
  44. if($params->keyword) {
  45. switch ($params->field) {
  46. case 'tb_post_tag.id' :
  47. case 'users.id' :
  48. case 'users.sid' :
  49. $q->where($params->field, '=', $params->keyword);
  50. break;
  51. case 'tb_post.subject' :
  52. case 'tb_post_tag.tag' :
  53. case 'users.name' :
  54. case 'users.email' :
  55. $q->where($params->field, 'LIKE', "%{$params->keyword}%");
  56. break;
  57. }
  58. }
  59. if($params->startDate || $params->endDate) {
  60. if($params->startDate) {
  61. $q->where('tb_post_tag.created_at', '>=', $params->startDate . ' 00:00:00');
  62. }
  63. if($params->endDate) {
  64. $q->where('tb_post_tag.created_at', '<=', $params->endDate . ' 23:59:59');
  65. }
  66. }
  67. if($params->boardID) {
  68. $q->where('tb_post_tag.board_id', '=', $params->boardID);
  69. }
  70. $q->from('tb_post_tag');
  71. $q->join('tb_post', 'tb_post.id', '=', 'tb_post_tag.post_id');
  72. $q->join('tb_board', 'tb_board.id', '=', 'tb_post_tag.board_id');
  73. $q->leftJoin('users', 'users.id', '=', 'tb_post.user_id');
  74. }, 'TT');
  75. $query->select(
  76. 'TT.id',
  77. 'TT.boardName',
  78. 'TT.post_id',
  79. 'TT.board_id',
  80. 'TT.user_id',
  81. 'TT.created_at',
  82. 'TT.code',
  83. 'TT.subject',
  84. 'TT.sid',
  85. 'TT.name',
  86. DB::raw('GROUP_CONCAT(DISTINCT TT.tag SEPARATOR ", ") AS tag')
  87. );
  88. $query->groupBy('TT.id', 'TT.post_id', 'TT.board_id', 'TT.user_id', 'TT.created_at', 'TT.code', 'TT.subject', 'TT.sid', 'TT.name');
  89. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  90. $total = $this->count();
  91. $rows = $list->count();
  92. return (object)[
  93. 'total' => $total,
  94. 'rows' => $rows,
  95. 'list' => $list
  96. ];
  97. }
  98. /**
  99. * 게시글 태그 저장 (쉼표(,) 구분하여 값 삽입)
  100. */
  101. public function register(Post $post, ?string $tags): void
  102. {
  103. if ($tags) {
  104. foreach (explode(',', $tags) as $tag) {
  105. $tag = trim($tag);
  106. if ($tag) {
  107. $this->insert([
  108. 'board_id' => $post->board_id,
  109. 'post_id' => $post->id,
  110. 'tag' => $tag,
  111. 'ip_address' => IP_ADDRESS,
  112. 'user_agent' => USER_AGENT,
  113. 'created_at' => now()
  114. ]);
  115. }
  116. }
  117. $post->updateTagRows($post->id);
  118. }
  119. }
  120. /**
  121. * 게시글 태그 저장 (쉼표(,) 구분하여 값 삽입)
  122. */
  123. public function updater(Post $post, ?string $tags): void
  124. {
  125. // 기존 태그 삭제
  126. $this->remove($post);
  127. // 새로 등록
  128. $this->register($post, $tags);
  129. }
  130. /**
  131. * 게시글 태그 삭제
  132. */
  133. public function remove(Post $post): void
  134. {
  135. $this->where([
  136. ['board_id', $post->board_id],
  137. ['post_id', $post->id]
  138. ])->delete();
  139. }
  140. }