Skip to content

Commit 03357fb

Browse files
authored
feat(laravel): supports more Eloquent types (#6544)
1 parent 9c46162 commit 03357fb

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2020
use Illuminate\Database\Eloquent\Model;
2121
use Illuminate\Database\Eloquent\Relations\HasMany;
22+
use Illuminate\Support\Collection;
2223
use Symfony\Component\PropertyInfo\Type;
2324

2425
/**
@@ -67,10 +68,15 @@ public function create(string $resourceClass, string $property, array $options =
6768
continue;
6869
}
6970

71+
// see https://laravel.com/docs/11.x/eloquent-mutators#attribute-casting
7072
$builtinType = $p['cast'] ?? $p['type'];
7173
$type = match ($builtinType) {
72-
'datetime', 'date' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTime::class),
74+
'double', 'real' => new Type(Type::BUILTIN_TYPE_FLOAT, $p['nullable']),
75+
'datetime', 'date', 'timestamp' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTime::class),
7376
'immutable_datetime', 'immutable_date' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTimeImmutable::class),
77+
'collection', 'encrypted:collection' => new Type(Type::BUILTIN_TYPE_ITERABLE, $p['nullable'], Collection::class, true),
78+
'encrypted:array' => new Type(Type::BUILTIN_TYPE_ARRAY, $p['nullable']),
79+
'encrypted:object' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable']),
7480
default => new Type(\in_array($builtinType, Type::$builtinTypes, true) ? $builtinType : Type::BUILTIN_TYPE_STRING, $p['nullable']),
7581
};
7682

0 commit comments

Comments
 (0)