@@ -3143,6 +3143,32 @@ public function testModelToJsonSucceedsWithPriorErrors(): void
3143
3143
3144
3144
$ this ->assertSame ('{"name":"Mateus"} ' , $ user ->toJson (JSON_THROW_ON_ERROR ));
3145
3145
}
3146
+
3147
+ public function testFillableWithMutators ()
3148
+ {
3149
+ $ model = new EloquentModelWithMutators ;
3150
+ $ model ->fillable (['full_name ' , 'full_address ' ]);
3151
+ $ model ->fill (['id ' => 1 , 'full_name ' => 'John Doe ' , 'full_address ' => '123 Main Street, Anytown ' ]);
3152
+
3153
+ $ this ->assertNull ($ model ->id );
3154
+ $ this ->assertSame ('John ' , $ model ->first_name );
3155
+ $ this ->assertSame ('Doe ' , $ model ->last_name );
3156
+ $ this ->assertSame ('123 Main Street ' , $ model ->address_line_one );
3157
+ $ this ->assertSame ('Anytown ' , $ model ->address_line_two );
3158
+ }
3159
+
3160
+ public function testGuardedWithMutators ()
3161
+ {
3162
+ $ model = new EloquentModelWithMutators ;
3163
+ $ model ->guard (['id ' ]);
3164
+ $ model ->fill (['id ' => 1 , 'full_name ' => 'John Doe ' , 'full_address ' => '123 Main Street, Anytown ' ]);
3165
+
3166
+ $ this ->assertNull ($ model ->id );
3167
+ $ this ->assertSame ('John ' , $ model ->first_name );
3168
+ $ this ->assertSame ('Doe ' , $ model ->last_name );
3169
+ $ this ->assertSame ('123 Main Street ' , $ model ->address_line_one );
3170
+ $ this ->assertSame ('Anytown ' , $ model ->address_line_two );
3171
+ }
3146
3172
}
3147
3173
3148
3174
class EloquentTestObserverStub
@@ -3870,3 +3896,35 @@ protected function stepOut(): void
3870
3896
}
3871
3897
}
3872
3898
}
3899
+
3900
+ class EloquentModelWithMutators extends Model
3901
+ {
3902
+ public $ attributes = [
3903
+ 'first_name ' => null ,
3904
+ 'last_name ' => null ,
3905
+ 'address_line_one ' => null ,
3906
+ 'address_line_two ' => null ,
3907
+ ];
3908
+
3909
+ protected function fullName (): Attribute
3910
+ {
3911
+ return Attribute::make (
3912
+ set: function (string $ fullName ) {
3913
+ [$ firstName , $ lastName ] = explode (' ' , $ fullName );
3914
+
3915
+ return [
3916
+ 'first_name ' => $ firstName ,
3917
+ 'last_name ' => $ lastName ,
3918
+ ];
3919
+ }
3920
+ );
3921
+ }
3922
+
3923
+ public function setFullAddressAttribute ($ fullAddress )
3924
+ {
3925
+ [$ addressLineOne , $ addressLineTwo ] = explode (', ' , $ fullAddress );
3926
+
3927
+ $ this ->attributes ['address_line_one ' ] = $ addressLineOne ;
3928
+ $ this ->attributes ['address_line_two ' ] = $ addressLineTwo ;
3929
+ }
3930
+ }
0 commit comments