Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

abkrim's avatar
Level 13

Using ULID in binary format

I want to use ULIDs in my models but store them in binary mode.

My question is if it is possible to use this way, knowing that my models have already applied the setters and getters for the binary/string conversion and in the boot when a model is created it is generated automatically

class User extends Model
{
    public $incrementing = false;
    protected $keyType = 'string';

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->{$model->getKeyName()} = (string) new Ulid();
        });
    }

    public function setIdAttribute($value)
    {
        $this->attributes['id'] = Ulid::fromString($value)->toBinary();
    }
    
    public function getIdAttribute($value)
    {
        return Ulid::fromBinary($value)->toString();
    }
0 likes
3 replies
abkrim's avatar
Level 13

Very old response. Not valid for laravel 10.

use Ulid\Ulid;  // NOt valid today.
use Symfony\Component\Uid\Ulid; // Possible?

???

Please or to participate in this conversation.