Document.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Models\DTO\SearchData;
  5. class Document extends Model
  6. {
  7. protected $table = 'tb_document';
  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 = 'updated_at';
  14. const DELETED_AT = null;
  15. protected $guarded = [];
  16. public function user()
  17. {
  18. return $this->belongsTo(User::class)->withDefault();
  19. }
  20. /**
  21. * 문서 목록 조회
  22. */
  23. public function data(SearchData $params): object
  24. {
  25. $query = $this->query();
  26. $query->select('tb_document.*');
  27. if ($params->keyword) {
  28. switch ($params->field) {
  29. case 'tb_document.subject' :
  30. case 'tb_document.content' :
  31. case 'users.name' :
  32. case 'users.email' :
  33. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  34. break;
  35. case 'tb_document.code' :
  36. $query->where($params->field, 'LIKE', "{$params->keyword}%");
  37. break;
  38. case 'tb_document.id' :
  39. case 'users.id' :
  40. $query->where($params->field, '=', $params->keyword);
  41. break;
  42. }
  43. }
  44. $query->leftJoin('users', 'users.id', '=', 'tb_document.user_id');
  45. $query->orderByDesc('tb_document.id');
  46. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  47. $total = $this->count();
  48. $rows = $list->count();
  49. return (object)[
  50. 'total' => $total,
  51. 'rows' => $rows,
  52. 'list' => $list
  53. ];
  54. }
  55. /**
  56. * 문서 정보 조회
  57. */
  58. public function findByCode(string $code): Document
  59. {
  60. return $this->where('code', $code)->firstOrNew();
  61. }
  62. }