Skip to content

Commit 30d9e87

Browse files
Add UUID initialization in model save method
1 parent 2476ecb commit 30d9e87

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

‎src/Model/Concerns/HasUuids.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,27 @@ public function getIncrementing()
6363

6464
return $this->incrementing;
6565
}
66+
67+
/**
68+
* Initialize the model with unique identifiers.
69+
*
70+
* @param array $columns
71+
* @return void
72+
*/
73+
protected function initialize(array $columns): void
74+
{
75+
foreach ($columns as $column) {
76+
if (in_array($column, $this->uniqueIds()) && ! $this->{$column}) {
77+
$this->{$column} = $this->newUniqueId();
78+
}
79+
}
80+
}
81+
82+
public function save(array $options = []): bool
83+
{
84+
$this->initialize($this->uniqueIds());
85+
86+
return parent::save($options);
87+
}
6688
}
89+

0 commit comments

Comments
 (0)