File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 3
3
All notable changes to this project will be documented in this file. This project adheres to
4
4
[ Semantic Versioning] ( http://semver.org/ ) and [ this changelog format] ( http://keepachangelog.com/ ) .
5
5
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
+
6
13
## [ 2.2.0] - 2022-12-22
7
14
8
15
### Added
Original file line number Diff line number Diff line change @@ -91,6 +91,28 @@ public function store(array $validatedData): object
91
91
{
92
92
$ model = $ this ->hydrate ($ validatedData );
93
93
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
+
94
116
/**
95
117
* Always do eager loading, as we may have default eager
96
118
* load paths.
You can’t perform that action at this time.
0 commit comments