skip to Main Content

I’m currently develop a web application with Laravel.
My database engine is a Microsoft SQL Server.

For some data I preferred to generate an uuid.
When I use Windows to run my Laravel app, the uuid format is correct :

  • A5EE121A-1F10-46FC-B779-49D2A0FA3B68

But when I ran my Laravel app under linux, and use the same database, the uuid format is like this :

  • b"x1Ax12î¥x10x1FüF·yIÒ ú;h"

I don’t know where is the problem…
Have you an idea ?

Thanks.

The goal is to retrieve the same format when the Laravel app ran under Windows and under Linux.

2

Answers


  1. In Microsoft SQL Server, UUIDs are typically stored using the UNIQUEIDENTIFIER datatype, which is a 16-byte binary value. When you fetch this value from the database, the driver might represent this binary data differently based on the platform.

    • On Windows, when you fetch the UUID from the database, the database driver is likely converting it to its string representation (smth like A5EE121A-1F10-46FC-B779-49D2A0FA3B68).
    • On Linux, the database driver might be fetching the raw binary data without converting it to its string representation. As a result, you’re seeing the raw bytes (example: b"x1Ax12î¥x10x1FüF·yIÒ ú;h").

    To resolve this and retrieve the UUID in a consistent format across both platforms, you can explicitly convert the UUID to its string representation when querying the database.

    SELECT CONVERT(NVARCHAR(36), [YourUUIDColumn]) AS uuid_string FROM [YourTableName]
    

    In your Laravel model, you can use accessors to handle the UUID conversion:

    public function getUuidAttribute($value)
    {
        return strtoupper(bin2hex($value));
    }
    

    so you can access it in Laravel using $model->uuid and get the expected string format.

    Login or Signup to reply.
  2. in your config/database.php add this

    'options' => [
    PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER => true,
    ],
    

    enter image description here
    then optimize:clear your app

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search