#Eloquent: `originalIsEquivalent()` two objects are not equal because the order is changed somewhere.

2 messages · Page 1 of 1 (latest)

worthy lake
#

Hey,

I have a Model that saves an array of services via an Attribute:

public function services(): Attribute
{
    return Attribute::make(
        get: fn ($value) => collect(json_decode($value, true) ?? [])->map(function ($data) {
            return PropertyService::fromArray($data);
        }),
        set: fn ($value) => $value->isEmpty() ? null : $value->map(fn ($value) => $value->toArray())->toJson(),
    );
}

When I save the exact same "service", Laravel detects it as isDirty(), because the string representation differs:

This is $property->getOriginal('services'):

[{"type": 5, "active": false, "end_date": "2016-12-30T23:00:00.000000Z", "start_date": "2023-11-28T10:41:02.236125Z"}

And this is the attribute setter: collect($property->services)->map(fn($item) => $item->toArray())->toJson()

[{"type":5,"active":false,"start_date":"2023-11-28T10:41:02.236125Z","end_date":"2016-12-30T23:00:00.000000Z"}

So, it seems that it doesn't save the exact value of the setter to the DB. What could mess with my string?!

worthy lake
#

Bump