Skip to content

Commit

Permalink
🎨 update readme to reflect finding the types in the attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Mar 17, 2022
1 parent 35e9778 commit 4a69c9e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
8 changes: 2 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,9 @@ declare global {
### Laravel V9 Attribute support
Laravel now has a very different way of [accessors and mutators](https://laravel.com/docs/9.x/eloquent-mutators#accessors-and-mutators)
In order to tell modeltyper the types of your attributes - we need a way to know - and will look for an $attrs array in your model:
In order to tell modeltyper the types of your attributes - be sure to specify the type the attribute returns:
```php
public array $attrs = [
'is_captain' => 'boolean',
];

/**
* Determine if the user is a captain of a team
*
Expand All @@ -151,7 +147,7 @@ In order to tell modeltyper the types of your attributes - we need a way to know
public function isCaptain(): Attribute
{
return Attribute::make(
get: fn () => $this->teams[0]->pivot->captain ?? false,
get: fn (): bool => $this->teams[0]->pivot->captain ?? false,
);
}
```
Expand Down
6 changes: 3 additions & 3 deletions src/ModelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,13 @@ public function getMutators(Model $model): array
$mutations[$mutator] = $model->attrs[$mutator];
continue;
}

$closure = call_user_func($reflection->getClosure($model), 1);
if (! is_null($closure->get)) {
if (!is_null($closure->get)) {
$rf = new ReflectionFunction($closure->get);
if ($rf->hasReturnType()) {
$returnType = $rf->getReturnType()->getName();
$mutations[$mutator] = $this->mapReturnType((string) $returnType);
$mutations[$mutator] = $this->mapReturnType($returnType);
continue;
}else {
// warn user to add return type to closure
Expand Down

0 comments on commit 4a69c9e

Please sign in to comment.