Skip to content
This repository was archived by the owner on Mar 12, 2024. It is now read-only.

Commit 51b3c67

Browse files
committed
nullable case
1 parent d6cd04a commit 51b3c67

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/Traits/WithModelMapping.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ trait WithModelMapping
1111
/**
1212
* Maps your model attributes to local class properties.
1313
*
14-
* @param Model $model
14+
* @param Model|null $model
1515
*
1616
* @return void
1717
*/
18-
public function mapModelAttributes(Model $model): void
18+
public function mapModelAttributes(?Model $model = null): void
1919
{
20-
collect($model->getAttributes())->each(function ($value, $property) {
21-
if (property_exists($this, $property)) {
22-
rescue(fn () => $this->{$property} = $value);
23-
}
24-
});
20+
if (! is_null($model)) {
21+
collect($model->getAttributes())->each(function ($value, $property) {
22+
if (property_exists($this, $property)) {
23+
rescue(fn () => $this->{$property} = $value);
24+
}
25+
});
26+
}
2527
}
2628
}

0 commit comments

Comments
 (0)