~/docs
Resonate
Resonate
9.x 9.0.0, current 8.x 8.0.0 7.x 7.0.1 6.x 6.2.1 5.x 5.1.2 4.x 4.0.2
Laravel UUID
Toggle sidebar
Laravel UUID
webpatser/laravel-uuid 7.0.1
You are reading the 7.x documentation
The current line is 9.x. Read this page for 9.x

Laravel UUID 7.x

SQL Server

Convert between standard UUIDs and SQL Server uniqueidentifier values with the Str SQL Server macros.

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'); // true
Str::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 uniqueidentifier and bytea column types that Laravel's schema grammar does not know, so this migration fails on sqlsrv and pgsql. On SQL Server use $table->uuid(), which Laravel maps to uniqueidentifier; 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