Skip to content

Commit 00d1fc5

Browse files
committed
Merge branch 'release/2.2.1'
2 parents e0d2ac7 + e75247c commit 00d1fc5

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
All notable changes to this project will be documented in this file. This project adheres to
44
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
55

6+
## [2.2.1] - 2023-01-23
7+
8+
### Fixed
9+
10+
- [laravel#223](https://github.com/laravel-json-api/laravel/issues/223) Ensure a model always has fresh data from the
11+
database after a write operation, to prevent stale data on cached relationships.
12+
613
## [2.2.0] - 2022-12-22
714

815
### Added

src/Hydrators/ModelHydrator.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ public function store(array $validatedData): object
9191
{
9292
$model = $this->hydrate($validatedData);
9393

94+
/**
95+
* Always refresh the model from the database.
96+
*
97+
* Due to the way Eloquent models cache relationships, it is
98+
* possible that a relationship on the model holds stale data.
99+
* E.g. if the relationship has been accessed on the model before
100+
* hydration was executed above, there is a possibility that some
101+
* data might be stale.
102+
*
103+
* We therefore always need to ensure we have the latest data from
104+
* the database, and a refresh is the only way to achieve that at
105+
* this point.
106+
*
107+
* This effectively means we are following more of a CQRS pattern.
108+
* I.e. we've done the "write" (command), now we're doing a query
109+
* so we need fresh data for that to achieve responsibility separation.
110+
* We'll move to a CQRS pattern in a future version of the package.
111+
*
112+
* @see https://github.com/laravel-json-api/laravel/issues/223
113+
*/
114+
$model->refresh();
115+
94116
/**
95117
* Always do eager loading, as we may have default eager
96118
* load paths.

0 commit comments

Comments
 (0)