Skip to content

Commit 4e7c71c

Browse files
[8.x] Only add getRouteKeyName to models when collection uses slugs (#677)
1 parent 84f7b9c commit 4e7c71c

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/Console/Commands/ImportCollection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ private function createEloquentModel(): self
162162
->filter(fn ($column) => in_array($column['type'], ['json', 'boolean', 'datetime', 'date', 'time', 'float', 'integer']))
163163
->map(fn ($column) => " '{$column['name']}' => '{$column['type']}',")
164164
->join(PHP_EOL))
165+
->when($this->collection->requiresSlugs(), function ($str) {
166+
return $str->replaceLast('}', <<<'PHP'
167+
168+
public function getRouteKeyName(): string
169+
{
170+
return 'slug';
171+
}
172+
}
173+
PHP);
174+
})
165175
->__toString();
166176

167177
File::put(app_path("Models/{$this->modelName}.php"), $modelContents);

src/Console/Commands/stubs/model-l10.stub

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,4 @@ class {{ class }} extends Model
3131
protected $casts = [
3232
{{ casts }}
3333
];
34-
35-
public function getRouteKeyName(): string
36-
{
37-
return 'slug';
38-
}
3934
}

src/Console/Commands/stubs/model.stub

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,4 @@ class {{ class }} extends Model
3434
{{ casts }}
3535
];
3636
}
37-
38-
public function getRouteKeyName(): string
39-
{
40-
return 'slug';
41-
}
4237
}

0 commit comments

Comments
 (0)