SQL Server stores a uniqueidentifier with mixed endianness: the first three groups are byte-swapped compared to a standard UUID, the last two are not. The package provides Str macros that apply this conversion, backed by Uuid::importFromSqlServer(), toSqlServer() and toSqlServerBinary() in webpatser/uuid.
Converting between formats
use Illuminate\Support\Str; // From SQL Server to a standard UUID$sqlServerGuid = '825B076B-44EC-E511-80DC-00155D0ABC54';$uuid = Str::uuidFromSqlServer($sqlServerGuid);// 6b075b82-ec44-11e5-80dc-00155d0abc54 // From a standard UUID to SQL Server format$guid = Str::uuidToSqlServer('6B075B82-EC44-11E5-80DC-00155D0ABC54');// 825B076B-44EC-E511-80DC-00155D0ABC54
Binary variants work on 16-byte strings:
$sqlServerBinary = Str::uuidToSqlServerBinary($uuid); // 16 bytes in SQL Server byte order$uuid = Str::sqlServerBinaryToUuid($sqlServerBinary); // back to a standard UUID string
| Macro | Behavior |
|---|---|
uuidFromSqlServer(string $sqlServerGuid) |
SQL Server GUID text to standard UUID string |
uuidToSqlServer(string $uuid) |
standard UUID to SQL Server GUID text; throws InvalidArgumentException on an invalid UUID |
uuidToSqlServerBinary(string $uuid) |
standard UUID to 16 bytes in SQL Server byte order; throws on an invalid UUID |
sqlServerBinaryToUuid(string $binary) |
16 bytes in SQL Server byte order to UUID string; throws unless the input is 16 bytes |
isSqlServerGuid(string $guid) |
true for a well-formed GUID string |
isSqlServerGuid() checks the text format only. A uniqueidentifier uses the same 8-4-4-4-12 hex text as a standard UUID and only the stored byte order differs, so the check is the same as Uuid::validate(). It cannot tell you which byte order a value is in.
Migrations
BinaryUuidMigrations creates binary(16) columns on MySQL, MariaDB and SQLite. On sqlsrv it tries a uniqueidentifier column and on pgsql a bytea column:
Warning: In 8.0.0 the helpers produce
uniqueidentifierandbyteacolumn types that Laravel's schema grammar does not know, so this migration fails onsqlsrvandpgsql. Use$table->uuid()or$table->binary()directly on those drivers. The example below only works on MySQL, MariaDB and SQLite.
use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;use Webpatser\LaravelUuid\BinaryUuidMigrations; return new class extends Migration{ public function up(): void { Schema::create('users', function (Blueprint $table) { BinaryUuidMigrations::addBinaryUuidPrimary($table); BinaryUuidMigrations::addBinaryUuidColumn($table, 'parent_id', true); $table->string('name'); $table->string('email')->unique(); $table->timestamps(); }); }};
To convert an existing string column on SQL Server, getConversionSql() returns SQL that adds a {column}_guid column and fills it with CAST(... AS uniqueidentifier):
echo BinaryUuidMigrations::getConversionSql('users', 'id', 'sqlsrv');
Working with existing data
The text conversion applies to GUID text produced from the raw byte order (for example data exported from binary(16) storage). It does not apply to values read from or written to a uniqueidentifier column, which SQL Server converts itself.
use Illuminate\Support\Facades\DB;use Illuminate\Support\Str; $users = DB::select('SELECT id, name FROM users WHERE active = 1'); foreach ($users as $user) { $uuid = Str::uuidFromSqlServer($user->id); echo "User: {$user->name}, UUID: {$uuid}\n";} $sqlServerGuid = Str::uuidToSqlServer(Str::fastUuid()); DB::insert('INSERT INTO users (id, name, email) VALUES (?, ?, ?)', [ $sqlServerGuid, 'Jane Doe',]);
Next steps
- Binary Storage: the binary trait, cast and migration helpers.
- UUID library: the SQL Server methods on the
Uuidclass.