Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casting id to string when uuid = true #76

Closed
robsonek opened this issue Jun 26, 2024 · 5 comments
Closed

Casting id to string when uuid = true #76

robsonek opened this issue Jun 26, 2024 · 5 comments

Comments

@robsonek
Copy link
Contributor

robsonek commented Jun 26, 2024

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';
        }
    });
}
@overtrue
Copy link
Owner

overtrue commented Jul 3, 2024

Please provide more context, thanks.

@robsonek
Copy link
Contributor Author

robsonek commented Jul 3, 2024

$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"

@overtrue
Copy link
Owner

overtrue commented Jul 4, 2024

@robsonek You can try this:

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

@overtrue
Copy link
Owner

overtrue commented Jul 4, 2024

@robsonek
Copy link
Contributor Author

robsonek commented Jul 4, 2024

OK thanks. Works correctly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants