File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed
src/Illuminate/Database/Eloquent/Concerns
tests/Integration/Database Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -1637,7 +1637,13 @@ protected function mergeAttributesFromClassCasts()
1637
1637
protected function mergeAttributesFromAttributeCasts ()
1638
1638
{
1639
1639
foreach ($ this ->attributeCastCache as $ key => $ value ) {
1640
- $ callback = $ this ->{Str::camel ($ key )}()->set ?: function ($ value ) use ($ key ) {
1640
+ $ attribute = $ this ->{Str::camel ($ key )}();
1641
+
1642
+ if ($ attribute ->get && ! $ attribute ->set ) {
1643
+ continue ;
1644
+ }
1645
+
1646
+ $ callback = $ attribute ->set ?: function ($ value ) use ($ key ) {
1641
1647
$ this ->attributes [$ key ] = $ value ;
1642
1648
};
1643
1649
Original file line number Diff line number Diff line change @@ -177,6 +177,17 @@ public function testSettingRawAttributesClearsTheCastCache()
177
177
178
178
$ this ->assertSame ('117 Spencer St. ' , $ model ->address ->lineOne );
179
179
}
180
+
181
+ public function testCastsThatOnlyHaveGetterDoNotPeristAnythingToModelOnSave ()
182
+ {
183
+ $ model = new TestEloquentModelWithAttributeCast ;
184
+
185
+ $ model ->virtual ;
186
+
187
+ $ model ->getAttributes ();
188
+
189
+ $ this ->assertTrue (empty ($ model ->getDirty ()));
190
+ }
180
191
}
181
192
182
193
class TestEloquentModelWithAttributeCast extends Model
@@ -253,6 +264,15 @@ public function password(): Attribute
253
264
return hash ('sha256 ' , $ value );
254
265
});
255
266
}
267
+
268
+ public function virtual (): Attribute
269
+ {
270
+ return new Attribute (
271
+ function () {
272
+ return collect ();
273
+ }
274
+ );
275
+ }
256
276
}
257
277
258
278
class AttributeCastAddress
You can’t perform that action at this time.
0 commit comments