| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use App\Models\DTO\SearchData;
- class Document extends Model
- {
- protected $table = 'tb_document';
- protected $primaryKey = 'id';
- public $keyType = 'int';
- public $incrementing = true;
- public $timestamps = true;
- const CREATED_AT = 'created_at';
- const UPDATED_AT = 'updated_at';
- const DELETED_AT = null;
- protected $guarded = [];
- public function user()
- {
- return $this->belongsTo(User::class)->withDefault();
- }
- /**
- * 문서 목록 조회
- */
- public function data(SearchData $params): object
- {
- $query = $this->query();
- $query->select('tb_document.*');
- if ($params->keyword) {
- switch ($params->field) {
- case 'tb_document.subject' :
- case 'tb_document.content' :
- case 'users.name' :
- case 'users.email' :
- $query->where($params->field, 'LIKE', "%{$params->keyword}%");
- break;
- case 'tb_document.code' :
- $query->where($params->field, 'LIKE', "{$params->keyword}%");
- break;
- case 'tb_document.id' :
- case 'users.id' :
- $query->where($params->field, '=', $params->keyword);
- break;
- }
- }
- $query->leftJoin('users', 'users.id', '=', 'tb_document.user_id');
- $query->orderByDesc('tb_document.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 findByCode(string $code): Document
- {
- return $this->where('code', $code)->firstOrNew();
- }
- }
|