laravel-versionable icon indicating copy to clipboard operation
laravel-versionable copied to clipboard

Casting id to string when uuid = true

Open robsonek opened this issue 1 year ago • 2 comments

I have uuid = true and when I get data from the model 'id' is cast to int. Adding it to Version.php solves the problem

protected $casts = [
    'contents' => 'json',
    'id' => 'string',
];
protected static function booted()
{
    static::creating(function (Version $version) {
        if (\config('versionable.uuid')) {
            $version->{$version->getKeyName()} = $version->{$version->getKeyName()} ?: (string) Str::orderedUuid();
        }
    });

    static::retrieved(function (Version $version) {
        if (\config('versionable.uuid')) {
            $version->casts[$version->getKeyName()] = 'string';
        }
    });
}

robsonek avatar Jun 26 '24 17:06 robsonek

Please provide more context, thanks.

overtrue avatar Jul 03 '24 02:07 overtrue

$version = Version::where('id', '9c67bec3-e795-4555-b130-98468e3034db')->first();
dd($version->id); // output: 9

When I cast id to string or set variable public $incrementing = false (only when 'uuid' => true)

$version = Version::where('id', '9c67bec3-e795-4555-b130-98468e3034db')->first();
dd($version->id); // output: "9c67bec3-e795-4555-b130-98468e3034db"

robsonek avatar Jul 03 '24 18:07 robsonek

@robsonek You can try this:

public function getIncrementing()
{
    return !\config('versionable.uuid');
}

overtrue avatar Jul 04 '24 06:07 overtrue

@robsonek https://github.com/overtrue/laravel-versionable/releases/tag/5.2.2

overtrue avatar Jul 04 '24 06:07 overtrue

OK thanks. Works correctly!

robsonek avatar Jul 04 '24 07:07 robsonek