Skip to content

Commit 181ebb8

Browse files
committed
Preserve sub-namespace when generating Entity from Model (--return=entity)
1 parent e61e52f commit 181ebb8

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

system/Commands/Generators/ModelGenerator.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,23 @@ protected function prepare(string $class): string
114114
}
115115

116116
if ($return === 'entity') {
117-
$return = str_replace('Models', 'Entities', $class);
117+
// Build the fully-qualified entity class from the model class so
118+
// that the generated Entity keeps any sub-namespaces (eg. Admin).
119+
$entityClass = str_replace('Models', 'Entities', $class);
118120

119-
if (preg_match('/^(\S+)Model$/i', $return, $match) === 1) {
120-
$return = $match[1];
121+
if (preg_match('/^(\S+)Model$/i', $entityClass, $match) === 1) {
122+
$entityClass = $match[1];
121123

122124
if ($this->getOption('suffix')) {
123-
$return .= 'Entity';
125+
$entityClass .= 'Entity';
124126
}
125127
}
126128

127-
$return = '\\' . trim($return, '\\') . '::class';
128-
$this->call('make:entity', array_merge([$baseClass], $this->params));
129+
// Call the entity generator with the fully-qualified class name so
130+
// it ends up under the correct sub-namespace/folder (eg. Admin).
131+
$this->call('make:entity', array_merge([trim($entityClass, '\\')], $this->params));
132+
133+
$return = '\\' . trim($entityClass, '\\') . '::class';
129134
} else {
130135
$return = "'{$return}'";
131136
}

0 commit comments

Comments
 (0)