SQL Server stores a GUID with the first three groups in little-endian byte order and the last two in big-endian order. A GUID read from SQL Server as bytes therefore differs from the standard UUID with the same text. The Str macros convert between the two.
Convert GUID strings
use Illuminate\Support\Str; // From SQL Server to a standard UUID (lowercase)$sqlServerGuid = '825B076B-44EC-E511-80DC-00155D0ABC54';$uuid = Str::uuidFromSqlServer($sqlServerGuid);// '6b075b82-ec44-11e5-80dc-00155d0abc54' // From a standard UUID to the SQL Server form (uppercase)$guid = Str::uuidToSqlServer('6B075B82-EC44-11E5-80DC-00155D0ABC54');// '825B076B-44EC-E511-80DC-00155D0ABC54'
The conversion reverses the first 4 bytes, the next 2 and the next 2, and keeps the last 8 bytes. It is its own inverse, so a round trip returns the original value.
Str::uuidToSqlServer() throws InvalidArgumentException for a string that is not a valid UUID. Str::uuidFromSqlServer() throws when the input does not contain 32 hex digits.
Binary GUIDs
use Illuminate\Support\Str; $uuid = '6b075b82-ec44-11e5-80dc-00155d0abc54'; $sqlServerBinary = Str::uuidToSqlServerBinary($uuid); // 16 bytes in SQL Server byte order$backToString = Str::sqlServerBinaryToUuid($sqlServerBinary); // standard UUID string
Str::sqlServerBinaryToUuid() throws InvalidArgumentException unless the input is exactly 16 bytes.
Detect the format
Str::isSqlServerGuid() calls Uuid::isSqlServerFormat(). In this version it returns true for any valid UUID string, so it cannot tell SQL Server GUIDs from standard UUIDs.
Macros
| Macro | Input | Returns |
|---|---|---|
Str::uuidFromSqlServer($guid) |
SQL Server GUID string | Standard UUID string |
Str::uuidToSqlServer($uuid) |
Standard UUID string | SQL Server GUID string |
Str::uuidToSqlServerBinary($uuid) |
Standard UUID string | 16 bytes in SQL Server order |
Str::sqlServerBinaryToUuid($binary) |
16 bytes in SQL Server order | Standard UUID string |
Str::isSqlServerGuid($guid) |
GUID string | bool (true for any valid UUID) |
The core class has matching methods: Uuid::importFromSqlServer(), toSqlServer(), toSqlServerBinary() and isSqlServerFormat(). See the core UUID documentation.
Migrations
In 6.2.1 the BinaryUuidMigrations helpers throw on the sqlsrv driver, because the SQL Server grammar has no uniqueidentifier column type to resolve. Create the column with $table->uuid('id')->primary() instead, which Laravel maps to uniqueidentifier on SQL Server.
To convert an existing string column, BinaryUuidMigrations::getConversionSql('users', 'id', 'sqlsrv') returns SQL for SQL Server. It adds a uniqueidentifier column, fills it with CAST(... AS uniqueidentifier), checks the result and leaves the drop and rename as comments. See Binary Storage for the other drivers.
Models
The model setup is the same as for other databases:
use Illuminate\Database\Eloquent\Model;use Webpatser\LaravelUuid\BinaryUuidCast;use Webpatser\LaravelUuid\HasBinaryUuids; class User extends Model{ use HasBinaryUuids; protected $casts = [ 'id' => BinaryUuidCast::class, 'parent_id' => BinaryUuidCast::class, ]; protected $fillable = ['name', 'email'];}
Note:
BinaryUuidCastreads a 16-byte value or a 36-character string and writes 16 bytes. It does not apply the SQL Server byte order conversion. When you exchange GUIDs with SQL Server as text or bytes, convert them with the macros above.
Next steps
- Binary Storage: the binary UUID helpers on all drivers.
- Eloquent Models: string UUID keys.
- Core UUID documentation: the UUID class API.