File tree 9 files changed +56
-4
lines changed
9 files changed +56
-4
lines changed Original file line number Diff line number Diff line change @@ -132,6 +132,8 @@ public static function makeModelFromContract(EntryContract $source)
132
132
$ attributes ['data ' ]->put ('template ' , $ source ->template );
133
133
}
134
134
135
+ $ attributes ['data ' ] = $ attributes ['data ' ]->filter (fn ($ v ) => $ v !== null );
136
+
135
137
foreach ($ dataMappings as $ key ) {
136
138
$ attributes [$ key ] = $ data ->get ($ key );
137
139
}
Original file line number Diff line number Diff line change @@ -40,7 +40,7 @@ public static function makeModelFromContract(Contract $source)
40
40
'store ' => $ source ->store (),
41
41
'email ' => $ source ->email (),
42
42
'honeypot ' => $ source ->honeypot (),
43
- 'data ' => $ source ->data (),
43
+ 'data ' => $ source ->data ()-> filter ( fn ( $ v ) => $ v !== null ) ,
44
44
],
45
45
]);
46
46
}
Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ public function toModel()
35
35
$ model = $ class ::findOrNew ($ this ->id ());
36
36
37
37
return (! empty ($ model ->id )) ? $ model ->fill ([
38
- 'data ' => $ this ->data ,
38
+ 'data ' => $ this ->data -> filter ( fn ( $ v ) => $ v !== null ) ,
39
39
]) : $ model ->fill ([
40
40
'id ' => $ this ->id (),
41
41
'data ' => $ this ->data ,
Original file line number Diff line number Diff line change @@ -39,7 +39,7 @@ public static function makeModelFromContract(Contract $source)
39
39
'handle ' => $ source ->globalSet ()->handle (),
40
40
'locale ' => $ source ->locale ,
41
41
])->fill ([
42
- 'data ' => $ data ,
42
+ 'data ' => $ data-> filter ( fn ( $ v ) => $ v !== null ) ,
43
43
'origin ' => $ source ->hasOrigin () ? $ source ->origin ()->locale () : null ,
44
44
]);
45
45
}
Original file line number Diff line number Diff line change @@ -82,7 +82,7 @@ public static function makeModelFromContract(Contract $source)
82
82
])->fill ([
83
83
'slug ' => $ source ->slug (),
84
84
'uri ' => $ source ->uri (),
85
- 'data ' => $ data ,
85
+ 'data ' => collect ( $ data)-> filter ( fn ( $ v ) => $ v !== null ) ,
86
86
'updated_at ' => $ source ->lastModified (),
87
87
]);
88
88
}
Original file line number Diff line number Diff line change @@ -328,4 +328,22 @@ public function saving_an_entry_updates_the_uri()
328
328
$ this ->assertSame ('/the-new-slug ' , $ entry ->uri ());
329
329
$ this ->assertSame ('/the-new-slug ' , $ entry ->model ()->uri );
330
330
}
331
+
332
+ #[Test]
333
+ public function null_values_are_removed_from_data ()
334
+ {
335
+ Collection::make ('blog ' )->title ('blog ' )
336
+ ->routes ('{parent_uri}/{slug} ' )
337
+ ->save ();
338
+
339
+ $ entry = (new Entry )
340
+ ->id ('1.0 ' )
341
+ ->collection ('blog ' )
342
+ ->slug ('the-slug ' )
343
+ ->data (['foo ' => 'bar ' , 'null_value ' => null ]);
344
+
345
+ $ entry ->save ();
346
+
347
+ $ this ->assertArrayNotHasKey ('null_value ' , $ entry ->model ()->data );
348
+ }
331
349
}
Original file line number Diff line number Diff line change @@ -101,4 +101,18 @@ public function it_should_not_save_date_in_data()
101
101
$ this ->assertInstanceOf (Carbon::class, $ fresh ->date ());
102
102
$ this ->assertSame ($ fresh ->date ()->format ('u ' ), $ submission ->date ()->format ('u ' ));
103
103
}
104
+
105
+ #[Test]
106
+ public function null_values_are_removed_from_data ()
107
+ {
108
+ $ form = tap (Facades \Form::make ('test ' )->title ('Test ' ))
109
+ ->save ();
110
+
111
+ $ submission = tap ($ form ->makeSubmission ([
112
+ 'name ' => 'John Doe ' ,
113
+ 'null_value ' => null ,
114
+ ]))->save ();
115
+
116
+ $ this ->assertArrayNotHasKey ('null_value ' , $ submission ->model ()->data );
117
+ }
104
118
}
Original file line number Diff line number Diff line change @@ -71,4 +71,12 @@ public function it_stores_form_data()
71
71
72
72
$ this ->assertSame (['some ' => 'data ' ], Arr::get ($ form ->model (), 'settings.data ' ));
73
73
}
74
+
75
+ #[Test]
76
+ public function null_values_are_removed_from_data ()
77
+ {
78
+ $ form = tap (Facades \Form::make ('test ' )->title ('Test form ' )->data (['some ' => 'data ' , 'null_value ' => null ]))->save ();
79
+
80
+ $ this ->assertSame (['some ' => 'data ' ], Arr::get ($ form ->model (), 'settings.data ' ));
81
+ }
74
82
}
Original file line number Diff line number Diff line change @@ -30,6 +30,16 @@ public function it_doesnt_create_a_new_model_when_slug_is_changed()
30
30
$ this ->assertSame ('new-slug ' , TermModel::first ()->slug );
31
31
}
32
32
33
+ #[Test]
34
+ public function null_values_are_removed_from_data ()
35
+ {
36
+ Taxonomy::make ('test ' )->title ('test ' )->save ();
37
+
38
+ $ term = tap (TermFacade::make ('test-term ' )->taxonomy ('test ' )->data (['null_value ' => null ]))->save ();
39
+
40
+ $ this ->assertArrayNotHasKey ('null_value ' , $ term ->model ()->data );
41
+ }
42
+
33
43
#[Test]
34
44
public function it_saves_updated_at_value_correctly ()
35
45
{
You can’t perform that action at this time.
0 commit comments