Uuid returns as lowercase, written to database uppercase
CHGill opened this issue · 2 comments
Using UUID via a trait:
` <?php
namespace App;
use Webpatser\Uuid\Uuid;
trait Uuids
{
/**
* Boot function from laravel.
*/
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->{$model->getKeyName()} = Uuid::generate()->string;
});
}
}`
When creating a record, the uuid is stored in the database uppercase, but it is passed to the return route url in lowercase. When you click on the same record from a list view the url contains the uuid in uppercase. I did not think this was an issue until trying to use Laravel Scout and Algolia Search.
This causes Algolia to create two records in the index, one with a lower case uuid when the record is created and another with uppercase uuid when the record is modified. Only the uppercase uuid is searchable since it matches the database record.
Why is this happening and how can I force uppercase UUID only within the trait?
In what database and type of field are you storing the UUIDs?
In a mysql char(36) field the UUID is stored lowercase. Also when I echo in PHP it's lowercase.
If you always want uppercase UUIDs then just use mb_strtoupper(Uuid::generate()->string)
I am using MSSQL with sqlsrv. I made the modifications to the trait and it solved my issue. Thanks!