Skip to content

Commit c49e901

Browse files
[11.x] Implement HasV7Uuids to use with MariaDB native uuid data type (#52029)
* implement HasV7UUids * Implement all uuid4 methods to uuid7 for unified experience and add ability to define time for the uuid7 * formatting * formatting * remove property --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 95107a0 commit c49e901

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Eloquent\Concerns;
4+
5+
use Illuminate\Support\Str;
6+
7+
trait HasVersion7Uuids
8+
{
9+
use HasUuids;
10+
11+
/**
12+
* Generate a new UUID (version 7) for the model.
13+
*
14+
* @return string
15+
*/
16+
public function newUniqueId()
17+
{
18+
return (string) Str::uuid7();
19+
}
20+
}

src/Illuminate/Support/Str.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,19 @@ public static function uuid()
17361736
: Uuid::uuid4();
17371737
}
17381738

1739+
/**
1740+
* Generate a UUID (version 7).
1741+
*
1742+
* @param \DateTimeInterface|null $time
1743+
* @return \Ramsey\Uuid\UuidInterface
1744+
*/
1745+
public static function uuid7($time = null)
1746+
{
1747+
return static::$uuidFactory
1748+
? call_user_func(static::$uuidFactory)
1749+
: Uuid::uuid7($time);
1750+
}
1751+
17391752
/**
17401753
* Generate a time-ordered UUID.
17411754
*

tests/Support/SupportStrTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ public function testUuid()
11011101
{
11021102
$this->assertInstanceOf(UuidInterface::class, Str::uuid());
11031103
$this->assertInstanceOf(UuidInterface::class, Str::orderedUuid());
1104+
$this->assertInstanceOf(UuidInterface::class, Str::uuid7());
11041105
}
11051106

11061107
public function testAsciiNull()
@@ -1354,7 +1355,7 @@ public function testItCanSpecifyASequenceOfUuidsToUtilise()
13541355
{
13551356
Str::createUuidsUsingSequence([
13561357
0 => ($zeroth = Str::uuid()),
1357-
1 => ($first = Str::uuid()),
1358+
1 => ($first = Str::uuid7()),
13581359
// just generate a random one here...
13591360
3 => ($third = Str::uuid()),
13601361
// continue to generate random uuids...

0 commit comments

Comments
 (0)