Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
add uuid setter
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Feb 15, 2020
1 parent 2961fda commit eae21e8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
15 changes: 11 additions & 4 deletions src/Eloquent/Concerns/UsesUUID.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ public static function bootUsesUUID(): void
return;
}

$model->setAttribute(
$model->getUuidName(),
static::generateUniqueUuid()
);
$model->setUuid(static::generateUniqueUuid());
});
}

Expand Down Expand Up @@ -61,6 +58,16 @@ public function scopeByUuid(Builder $query, $uuid): Builder
throw new InvalidArgumentException('The UUID has to be of type string, array or null.');
}

/**
* @param string|UuidInterface $uuid
*
* @return Model
*/
public function setUuid($uuid): Model
{
return $this->setAttribute($this->getUuidName(), strval($uuid));
}

public function getUuid(): ?string
{
return $this->getAttribute($this->getUuidName());
Expand Down
5 changes: 2 additions & 3 deletions tests/UsesUuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ public function it_does_not_generate_uuid_on_creating_when_uuid_attribute_is_val
{
$uuid = Str::uuid()->toString();

$post = PostUuidAttribute::create([
'uuid' => $uuid,
]);
$post = new PostUuidAttribute();
$post->setUuid($uuid)->save();

static::assertIsString($post->getUuid());
static::assertTrue(Uuid::isValid($post->uuid));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function up(): void
{
Schema::create('posts', static function (Blueprint $table): void {
$table->bigIncrements('id');
$table->uuid('uuid');
$table->uuid('uuid')->unique();
$table->timestamps();
});
}
Expand Down

0 comments on commit eae21e8

Please sign in to comment.