Improve casts support (#1262)

* support for all possible `Model::$casts` types

* add testing for casts

* update changelog

* update changelog (again?)
This commit is contained in:
Michael Newton
2021-11-14 17:40:15 +01:00
committed by GitHub
parent 01be015f86
commit ebea96213d
6 changed files with 219 additions and 0 deletions
+17
View File
@@ -355,11 +355,24 @@ class ModelsCommand extends Command
{
$casts = $model->getCasts();
foreach ($casts as $name => $type) {
if (Str::startsWith($type, 'decimal:')) {
$type = 'decimal';
} elseif (Str::startsWith($type, 'custom_datetime:')) {
$type = 'date';
} elseif (Str::startsWith($type, 'immutable_custom_datetime:')) {
$type = 'immutable_date';
} elseif (Str::startsWith($type, 'encrypted:')) {
$type = Str::after($type, ':');
}
switch ($type) {
case 'encrypted':
$realType = 'mixed';
break;
case 'boolean':
case 'bool':
$realType = 'boolean';
break;
case 'decimal':
case 'string':
$realType = 'string';
break;
@@ -384,6 +397,10 @@ class ModelsCommand extends Command
case 'datetime':
$realType = $this->dateClass;
break;
case 'immutable_date':
case 'immutable_datetime':
$realType = '\Carbon\CarbonImmutable';
break;
case 'collection':
$realType = '\Illuminate\Support\Collection';
break;