Skip to content

Commit 6e2155c

Browse files
authored
fix(primaryKey): #844 add custom primary key from json file (#848)
1 parent 7d64291 commit 6e2155c

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/Generators/ModelGenerator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,15 @@ private function fillTemplate($templateData)
6161
$templateData = $this->fillSoftDeletes($templateData);
6262

6363
$fillables = [];
64+
$primaryKey = 'id';
6465

6566
foreach ($this->commandData->fields as $field) {
6667
if ($field->isFillable) {
6768
$fillables[] = "'".$field->name."'";
6869
}
70+
if ($field->isPrimary) {
71+
$primaryKey = $field->name;
72+
}
6973
}
7074

7175
$templateData = $this->fillDocs($templateData);
@@ -76,6 +80,9 @@ private function fillTemplate($templateData)
7680
$primary = infy_tab()."protected \$primaryKey = '".$this->commandData->getOption('primary')."';\n";
7781
} else {
7882
$primary = '';
83+
if ($this->commandData->getOption('fieldsFile') && $primaryKey != 'id') {
84+
$primary = infy_tab()."protected \$primaryKey = '".$primaryKey."';\n";
85+
}
7986
}
8087

8188
$templateData = str_replace('$PRIMARY$', $primary, $templateData);

0 commit comments

Comments
 (0)