eloquent-driver icon indicating copy to clipboard operation
eloquent-driver copied to clipboard

`fresh()` not returning new instance

Open helloiamlukas opened this issue 3 years ago • 4 comments

Description

If you are editing an entry, save it and try to get a new instance with the fresh() method you will get the outdated entry back.

Example

$entry->set('title', 'New Title');
$entry->save();

dd(
    $entry->fresh()->title,
    \Statamic\Facades\Entry::query()->where('id', $entry->id)->first()->title,
);

This will output two different values.

helloiamlukas avatar Oct 04 '22 09:10 helloiamlukas

The reason for this problem is the blink cache in the find() method.

https://github.com/statamic/eloquent-driver/blob/2c4a775accb30803120b009209bc80ebcca23093/src/Entries/EntryRepository.php#L20-L25

helloiamlukas avatar Oct 04 '22 09:10 helloiamlukas

I've opened a PR here: https://github.com/statamic/eloquent-driver/pull/64

ryanmitchell avatar Oct 04 '22 09:10 ryanmitchell

That was fast! I just wanted to submit a PR too, but I guess you were faster :) Thanks @ryanmitchell 🚀

helloiamlukas avatar Oct 04 '22 09:10 helloiamlukas

@helloiamlukas I feel responsible :)

ryanmitchell avatar Oct 04 '22 09:10 ryanmitchell