SmsBook.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 SmsBook extends Model
  7. {
  8. protected $table = 'tb_sms_book';
  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 = 'updated_at';
  15. const DELETED_AT = null;
  16. protected $guarded = [];
  17. public function smsUser()
  18. {
  19. return $this->hasMany(SmsUser::class);
  20. }
  21. /**
  22. * 연락처 그룹 조회
  23. */
  24. public function data(SearchData $params): object
  25. {
  26. $query = $this->query();
  27. $query->select(
  28. 'tb_sms_book.*',
  29. DB::raw('(SELECT FORMAT(COUNT(*), 0) FROM tb_sms_user WHERE sms_book_id = tb_sms_book.id) AS userCount')
  30. );
  31. $query->orderByDesc('tb_sms_book.order');
  32. $query->orderByDesc('tb_sms_book.created_at');
  33. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  34. $total = $this->count();
  35. $rows = $list->count();
  36. return (object)[
  37. 'total' => $total,
  38. 'rows' => $rows,
  39. 'list' => $list
  40. ];
  41. }
  42. }