SQL Server stores a uniqueidentifier with a mixed byte order: the first three groups are little-endian and the last two are big-endian. The same UUID therefore has a different byte layout in SQL Server than in other databases. The package provides Str macros that convert between the two layouts.
Converting text GUIDs
use Illuminate\Support\Str; $sqlServerGuid = '825B076B-44EC-E511-80DC-00155D0ABC54';$uuid = Str::uuidFromSqlServer($sqlServerGuid);// 6b075b82-ec44-11e5-80dc-00155d0abc54 (lowercase) $sqlServerFormat = Str::uuidToSqlServer('6B075B82-EC44-11E5-80DC-00155D0ABC54');// 825B076B-44EC-E511-80DC-00155D0ABC54
These values are the ones the package tests use. uuidFromSqlServer() and sqlServerBinaryToUuid() return lowercase text, while uuidToSqlServer() returns uppercase. uuidToSqlServer() throws InvalidArgumentException for an invalid UUID.
Converting binary values
use Illuminate\Support\Str; $standardUuid = '6B075B82-EC44-11E5-80DC-00155D0ABC54'; $sqlServerBinary = Str::uuidToSqlServerBinary($standardUuid); // 16 bytes$backToString = Str::sqlServerBinaryToUuid($sqlServerBinary);
sqlServerBinaryToUuid() throws InvalidArgumentException unless the input is exactly 16 bytes.
Checking a GUID string
use Illuminate\Support\Str; Str::isSqlServerGuid('825B076B-44EC-E511-80DC-00155D0ABC54'); // trueStr::isSqlServerGuid('not-a-guid'); // false
Since 7.0.1 this checks only that the string is a well-formed UUID text (8-4-4-4-12 hex), because the text form is the same in SQL Server and elsewhere; only the stored byte order differs. It cannot tell a SQL Server layout from a standard one. Before 7.0.1 the macro called a method that does not exist in the library and failed at runtime.
Migrations
BinaryUuidMigrations tries a uniqueidentifier column on the sqlsrv driver:
Warning: In 7.0.1 the helpers produce
uniqueidentifierandbyteacolumn types that Laravel's schema grammar does not know, so this migration fails onsqlsrvandpgsql. On SQL Server use$table->uuid(), which Laravel maps touniqueidentifier; on PostgreSQL use$table->binary(). 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() { Schema::create('users', function (Blueprint $table) { BinaryUuidMigrations::addBinaryUuidPrimary($table); BinaryUuidMigrations::addBinaryUuidColumn($table, 'parent_id', true); $table->string('name'); $table->string('email')->unique(); $table->timestamps(); }); }};
BinaryUuidMigrations::getConversionSql('users', 'id', 'sqlsrv') returns commented SQL that adds a uniqueidentifier column, fills it with CAST(... AS uniqueidentifier) and verifies the result. See Binary Storage for the other helpers.
Models
Models use the same traits as on other databases:
use Illuminate\Database\Eloquent\Model;use Webpatser\LaravelUuid\HasUuids; class User extends Model{ use HasUuids;}
Working with existing data
When you read GUIDs from SQL Server as text and need the standard UUID, convert with uuidFromSqlServer(). When you write a UUID that came from another source into a SQL Server column that the application reads in SQL Server's layout, convert with uuidToSqlServer():
use Illuminate\Support\Facades\DB;use Illuminate\Support\Str; $users = DB::select('SELECT id, name FROM users WHERE active = 1'); foreach ($users as $user) { $standardUuid = Str::uuidFromSqlServer($user->id);} DB::insert('INSERT INTO users (id, name, email) VALUES (?, ?, ?)', [ Str::uuidToSqlServer(Str::fastUuid()), 'Jane Doe',]);
The library's own docs describe the byte-level details: UUID library.
Next steps
- Binary Storage: binary columns on all supported databases.
- Eloquent Models: model setup.