Skip to content

Commit 5d34d92

Browse files
authoredJul 22, 2022
[9.x] Fixes for model:show when attribute default is an enum (#43370)
* Handle enum defaults in JSON output * Display value for BackedEnum attribute defaults
1 parent 569d7a2 commit 5d34d92

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed
 

‎src/Illuminate/Foundation/Console/ShowModelCommand.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Foundation\Console;
44

5+
use BackedEnum;
56
use Doctrine\DBAL\Schema\Column;
67
use Doctrine\DBAL\Schema\Index;
78
use Doctrine\DBAL\Types\DecimalType;
@@ -330,7 +331,7 @@ protected function displayCli($class, $database, $table, $attributes, $relations
330331

331332
if ($attribute['default'] !== null) {
332333
$this->components->bulletList(
333-
[sprintf('default: %s', $attribute['default'] instanceof UnitEnum ? $attribute['default']->name : $attribute['default'])],
334+
[sprintf('default: %s', $attribute['default'])],
334335
OutputInterface::VERBOSITY_VERBOSE
335336
);
336337
}
@@ -413,11 +414,17 @@ protected function getColumnType($column)
413414
*
414415
* @param \Doctrine\DBAL\Schema\Column $column
415416
* @param \Illuminate\Database\Eloquent\Model $model
416-
* @return string|null
417+
* @return mixed|null
417418
*/
418419
protected function getColumnDefault($column, $model)
419420
{
420-
return $model->getAttributes()[$column->getName()] ?? $column->getDefault();
421+
$attributeDefault = $model->getAttributes()[$column->getName()] ?? null;
422+
423+
return match (true) {
424+
$attributeDefault instanceof BackedEnum => $attributeDefault->value,
425+
$attributeDefault instanceof UnitEnum => $attributeDefault->name,
426+
default => $attributeDefault ?? $column->getDefault(),
427+
};
421428
}
422429

423430
/**

0 commit comments

Comments
 (0)