Level 13
Very old response. Not valid for laravel 10.
use Ulid\Ulid; // NOt valid today.
use Symfony\Component\Uid\Ulid; // Possible?
???
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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();
}
I think that is what you are looking for: https://github.com/michaeldyrynda/laravel-efficient-uuid
Just change UUID to ULID I suppose.
Hope it helps!
Please or to participate in this conversation.