File tree 2 files changed +27
-11
lines changed
2 files changed +27
-11
lines changed Original file line number Diff line number Diff line change @@ -250,18 +250,14 @@ protected function getAllAppends(): array
250
250
*/
251
251
protected function addRequestedModelFieldsToQuery ()
252
252
{
253
- $ modelTableName = $ this ->getSubject ()->getModel ()->getTable ();
254
- $ prepend_table_name = true ;
253
+ $ modelTableName = $ this ->getModel ()->getTable ();
255
254
256
- if ($ this ->request ->has ('fields ' )) {
257
- $ modelFields = $ this ->request ->fields ()->get ($ modelTableName );
255
+ $ fields = $ this ->request ->fields ();
258
256
259
- if (!$ modelFields ) {
260
- $ modelFields = $ this ->request ->fields ()->get (0 );
261
- }
262
- } else {
263
- //$prepend_table_name = false;
264
- $ modelFields = $ this ->allowedFields ->all ();
257
+ $ modelFields = $ fields ->has ($ modelTableName ) ? $ fields ->get ($ modelTableName ) : $ fields ->get ('_ ' );
258
+
259
+ if (empty ($ modelFields )) {
260
+ return ;
265
261
}
266
262
267
263
// get all append fields to include
@@ -288,7 +284,7 @@ protected function addRequestedModelFieldsToQuery()
288
284
return ;
289
285
}
290
286
291
- $ prependedFields = $ prepend_table_name ? $ this ->prependFieldsWithTableName ($ modelFields , $ modelTableName ) : $ modelFields ;
287
+ $ prependedFields = $ this ->prependFieldsWithTableName ($ modelFields , $ modelTableName );
292
288
293
289
// get rid of any appended fields present
294
290
$ prependedFields = array_diff (
Original file line number Diff line number Diff line change @@ -110,4 +110,24 @@ public function it_can_load_api_model_appends(): void
110
110
'slug ' => 'orange ' ,
111
111
]);
112
112
}
113
+
114
+ /** @test */
115
+ public function it_can_list_only_specific_api_model_fields (): void
116
+ {
117
+ $ this ->withoutExceptionHandling ();
118
+
119
+ $ product = Product::factory ()->create ([
120
+ 'name ' => 'Apple ' ,
121
+ 'slug ' => 'orange ' ,
122
+ ]);
123
+
124
+ $ this ->getJson ('/products?fields=id,name&append= ' )
125
+ ->assertSuccessful ()
126
+ ->assertJsonFragment ([
127
+ 'name ' => 'Apple ' ,
128
+ ])
129
+ ->assertJsonMissing ([
130
+ 'slug ' => 'orange ' ,
131
+ ]);
132
+ }
113
133
}
You can’t perform that action at this time.
0 commit comments