grestock's avatar

Datatype binary

There is a character datatype BINARY in mysql. https://dev.mysql.com/doc/refman/5.7/en/binary-varbinary.html

"The BINARY types are similar to CHAR (...) That is, they store byte strings rather than character strings."

I came across an application which stores UUIDs (with length 32, without hyphens) as table-id.

But if using the Laravel datatype BINARY in a migration, it creates a BLOB, which is different.

Any idea how I can use this id in Laravel?

Thanks Grestock

0 likes
4 replies
jlrdw's avatar

What do you want to store there? If storing UUID, then varchar field.

grestock's avatar

It is an ecommerce software based on symfony:

1 - they generate a hexadecimal UUID like 550e8400-e29b-11d4-a716-446655440000

2 - they strip the hyphens 550e8400e29b11d4a716446655440000

3 - store it as ID in a database as BINARY(16), where you can see and edit the entry as 0x550e8400e29b11d4a716446655440000

The reason is, that BINARY as an id ist faster than CHAR or VARCHAR

I am writing a product information system and would like to use the same ID principle. The benefit is, to first generate a product with ID in my database and send it to the ecommerce api. Then we have the same id in both systems.

sr57's avatar

You can do 'raw migration'

    public function up() {
		DB::select("alter table mytable add column f1 binary(32)");
    }
1 like
bmckay959's avatar
Level 8

Just want to post this here for anyone that comes across. It's possible with standard Laravel syntax using charset to do a 'normal' binary column.

$table->char('uuid', 16)->charset('binary');

1 like

Please or to participate in this conversation.