| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use App\Models\DTO\SearchData;
- class PostLike extends Model
- {
- protected $table = 'tb_post_like';
- protected $primaryKey = 'id';
- public $keyType = 'int';
- public $incrementing = true;
- public $timestamps = true;
- const CREATED_AT = 'created_at';
- const UPDATED_AT = null;
- protected $guarded = [];
- public function board()
- {
- return $this->belongsTo(Board::class)->withDefault();
- }
- public function post()
- {
- return $this->belongsTo(Post::class)->withDefault();
- }
- public function user()
- {
- return $this->belongsTo(User::class)->withDefault();
- }
- /**
- * 게시판 추천/비추 삭제
- */
- public function data(SearchData $params): object
- {
- $query = $this->query();
- $query->select(
- 'tb_board.name AS boardName',
- 'tb_board.code',
- 'tb_post.subject',
- 'tb_post_like.*',
- 'user_1.sid AS sid_1',
- 'user_1.name AS name_1',
- 'user_2.sid AS sid_2',
- 'user_2.name AS name_2'
- );
- if($params->keyword) {
- switch ($params->field) {
- case 'tb_post_like.ip_address' :
- case 'user_1.name' :
- case 'user_1.email' :
- case 'user_2.name' :
- case 'user_2.email' :
- $query->where($params->field, 'LIKE', "%{$params->keyword}%");
- break;
- case 'tb_post_like.id' :
- case 'tb_post_like.post_id' :
- case 'user_1.id' :
- case 'user_1.sid' :
- case 'user_2.id' :
- case 'user_2.sid' :
- $query->where($params->field, '=', $params->keyword);
- break;
- }
- }
- if($params->startDate || $params->endDate) {
- if($params->startDate) {
- $query->where('tb_post_like.created_at', '>=', $params->startDate . ' 00:00:00');
- }
- if($params->endDate) {
- $query->where('tb_post_like.created_at', '<=', $params->endDate . ' 23:59:59');
- }
- }
- if($params->boardID) {
- $query->where('tb_post_like.board_id', '=', $params->boardID);
- }
- $query->join('tb_board', 'tb_board.id', '=', 'tb_post_like.board_id');
- $query->join('tb_post', 'tb_post.id', '=', 'tb_post_like.post_id');
- $query->leftJoin('users AS user_1', 'user_1.id', '=', 'tb_post_like.user_id'); // 추천인
- $query->leftJoin('users AS user_2', 'user_2.id', '=', 'tb_post.user_id'); // 작성자
- $query->orderByDesc('tb_post_like.id');
- $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
- $total = $this->count();
- $rows = $list->count();
- return (object)[
- 'total' => $total,
- 'rows' => $rows,
- 'list' => $list
- ];
- }
- /**
- * 게시글 좋아요/싫어요 정보
- */
- public function info(Post $post, int $userID): PostLike
- {
- return $this->where([
- ['board_id', $post->board_id],
- ['post_id', $post->id],
- ['user_id', $userID]
- ])->firstOrNew();
- }
- /**
- * 게시글 좋아요/싫어요 등록
- */
- public function register(array $params): int
- {
- return $this->insertGetId($params);
- }
- /**
- * 게시글 좋아요/싫어요 삭제
- */
- public function remove(Post $post, int $userID): int
- {
- return $this->where([
- ['board_id', $post->board_id],
- ['post_id', $post->id],
- ['user_id', $userID]
- ])->delete();
- }
- /**
- * 회원이 좋아요/싫어요 둘중 어느것 선택했는지 확인
- */
- public function isAlready(Post $post, int $userID, int $type): bool
- {
- return $this->where([
- ['board_id', $post->board_id],
- ['post_id', $post->id],
- ['user_id', $userID],
- ['type', $type]
- ])->exists();
- }
- }
|