laravel-versionable
laravel-versionable copied to clipboard
Casting id to string when uuid = true
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';
}
});
}
Please provide more context, thanks.
$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 You can try this:
public function getIncrementing()
{
return !\config('versionable.uuid');
}
@robsonek https://github.com/overtrue/laravel-versionable/releases/tag/5.2.2
OK thanks. Works correctly!