Files
sam-api/app/Console/Commands/MakeModelWithRelationships.php
hskwon 1a796462e4 feat: ERD 자동 생성 시스템 구축 및 모델 오류 수정
- GraphViz 설치를 통한 ERD 다이어그램 생성 지원
- BelongsToTenantTrait → BelongsToTenant 트레잇명 수정
- Estimate, EstimateItem 모델의 인터페이스 참조 오류 해결
- 60개 모델의 완전한 관계도 생성 (graph.png, 4.1MB)
- beyondcode/laravel-er-diagram-generator 패키지 활용

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-24 22:30:28 +09:00

76 lines
2.3 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
class MakeModelWithRelationships extends Command
{
protected $signature = 'make:model-with-docs {name} {--migration} {--controller} {--resource}';
protected $description = '모델 생성 후 자동으로 관계 문서 업데이트';
public function handle()
{
$modelName = $this->argument('name');
// 기본 모델 생성
$options = [];
if ($this->option('migration')) $options['--migration'] = true;
if ($this->option('controller')) $options['--controller'] = true;
if ($this->option('resource')) $options['--resource'] = true;
Artisan::call('make:model', array_merge(['name' => $modelName], $options));
$this->info("✅ 모델 생성 완료: {$modelName}");
// 관계 템플릿 추가
$this->addRelationshipTemplate($modelName);
// 논리적 관계 문서 업데이트
Artisan::call('db:update-relationships');
$this->info('✅ 논리적 관계 문서 업데이트 완료');
}
private function addRelationshipTemplate(string $modelName): void
{
$modelPath = app_path("Models/{$modelName}.php");
if (!File::exists($modelPath)) {
$this->error("모델 파일을 찾을 수 없습니다: {$modelPath}");
return;
}
$content = File::get($modelPath);
// 관계 메서드 템플릿 추가
$template = "
// ========================================
// 관계 메서드 (Relationships)
// ========================================
//
// 예시:
// public function category()
// {
// return \$this->belongsTo(Category::class);
// }
//
// public function items()
// {
// return \$this->hasMany(Item::class);
// }
//
// ⚠️ 새 관계 추가 후 'php artisan db:update-relationships' 실행
";
// 클래스 끝 부분에 템플릿 삽입
$content = str_replace(
'}' . PHP_EOL,
$template . PHP_EOL . '}' . PHP_EOL,
$content
);
File::put($modelPath, $content);
$this->info("📝 관계 템플릿 추가: {$modelName}");
}
}