'array', 'is_active' => 'boolean', 'created_by' => 'integer', 'updated_by' => 'integer', ]; /** * 활성화된 플로우만 조회 */ public function scopeActive($query) { return $query->where('is_active', true); } /** * 카테고리로 필터링 */ public function scopeCategory($query, string $category) { return $query->where('category', $category); } /** * 관계: 실행 이력 */ public function runs(): HasMany { return $this->hasMany(AdminApiFlowRun::class, 'flow_id'); } /** * 관계: 생성자 */ public function creator(): BelongsTo { return $this->belongsTo(User::class, 'created_by'); } /** * 관계: 수정자 */ public function updater(): BelongsTo { return $this->belongsTo(User::class, 'updated_by'); } /** * 관계: 최신 실행 기록 (서브쿼리 방식 - window function 회피) */ public function latestRun(): HasOne { return $this->hasOne(AdminApiFlowRun::class, 'flow_id') ->whereIn('id', function ($query) { $query->selectRaw('MAX(id)') ->from('admin_api_flow_runs') ->groupBy('flow_id'); }); } /** * 최근 실행 결과 조회 (단일 조회용) */ public function getLatestRunResult(): ?AdminApiFlowRun { return $this->runs()->latest('created_at')->first(); } /** * 플로우 정의에서 스텝 수 반환 */ public function getStepCountAttribute(): int { return count($this->flow_definition['steps'] ?? []); } }