Skip to content

Commit

Permalink
Merge pull request #385 from martio/fix/laravel-data-model-normalizer
Browse files Browse the repository at this point in the history
Add support for models without all columns
  • Loading branch information
rubenvanassche authored Mar 24, 2023
2 parents 869b876 + 5d34412 commit b6b9d96
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Normalizers/ModelNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ public function normalize(mixed $value): ?array
$properties = $value->toArray();

foreach ($value->getDates() as $key) {
$properties[$key] = $value->getAttribute($key);
if (isset($properties[$key])) {
$properties[$key] = $value->getAttribute($key);
}
}

foreach ($value->getCasts() as $key => $cast) {
if ($this->isDateCast($cast)) {
$properties[$key] = $value->getAttribute($key);
if (isset($properties[$key])) {
$properties[$key] = $value->getAttribute($key);
}
}
}

foreach ($value->getRelations() as $key => $relation) {
$key = $value::$snakeAttributes ? Str::snake($key) : $key;

$properties[$key] = $relation;
if (isset($properties[$key])) {
$properties[$key] = $relation;
}
}

return $properties;
Expand Down

0 comments on commit b6b9d96

Please sign in to comment.