Skip to content

Commit c55e357

Browse files
committed
check model to create migration
1 parent 98a98e1 commit c55e357

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/Commands/LaravelRelationMakerCommand.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ public function handle()
1717
$modelB = $this->argument('modelB');
1818
$relation = $this->argument('relation');
1919

20-
//$this->info(' > Checking ' . $this->qualifyModel($modelA));
20+
// If Model A exists
2121
if (file_exists($this->getPath($this->qualifyClass($modelA)))) {
22-
// 代表是把原本有的東東新增
22+
// Check if relation function (likes "cars" or "car")
2323
if (method_exists($this->qualifyClass($modelA), strtolower($modelB)) || method_exists($this->qualifyClass($modelA), strtolower($modelB) . 's')) {
2424
$this->error('Method exists.');
25-
2625
return 1;
2726
}
27+
28+
// [A1] Adding relation function
2829
$content = file_get_contents($this->getPath($this->qualifyClass($modelA)));
2930
$str = $this->str_lreplace('}', (<<<FUNC
3031
public function {{ modelName }}(){
@@ -34,13 +35,15 @@ public function {{ modelName }}(){
3435
FUNC), $content);
3536
file_put_contents($this->getPath($this->qualifyClass($modelA)), $str);
3637
$this->info(' # Modified ' . $this->qualifyClass($modelA));
38+
// [/A1]
3739
} else {
40+
// Model A not exists => call parent create model files
3841
parent::handle();
3942
$this->info(' # Created ' . $this->qualifyClass($modelA));
4043
}
4144

45+
// Replace model name (if model exists, then it'll not doing so.)
4246
$content = file_get_contents($this->getPath($this->qualifyClass($modelA)));
43-
4447
$str = str_replace('{{ model }}', $modelB, $content);
4548

4649
if ($relation === 'hasMany') {
@@ -112,7 +115,14 @@ public function {{ modelName }}(){
112115
############
113116
## Normal ##
114117
############
115-
$arr = [$modelA,$modelB];
118+
$arr = [];
119+
if (!file_exists($this->getPath($this->qualifyClass($modelA)))){
120+
array_push($arr, $modelA);
121+
}
122+
if (!file_exists($this->getPath($this->qualifyClass($modelB)))){
123+
array_push($arr, $modelB);
124+
}
125+
116126
foreach ($arr as $model) {
117127
$file = file_get_contents($this->getParsedStub('migration.relation.stub'));
118128
$this->clearMigration();

0 commit comments

Comments
 (0)