Skip to content

Commit 2855bff

Browse files
committed
- Fixed query builder select fields
1 parent 5610cc8 commit 2855bff

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

Diff for: src/QueryBuilder.php

+7-11
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,14 @@ protected function getAllAppends(): array
250250
*/
251251
protected function addRequestedModelFieldsToQuery()
252252
{
253-
$modelTableName = $this->getSubject()->getModel()->getTable();
254-
$prepend_table_name = true;
253+
$modelTableName = $this->getModel()->getTable();
255254

256-
if ($this->request->has('fields')) {
257-
$modelFields = $this->request->fields()->get($modelTableName);
255+
$fields = $this->request->fields();
258256

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;
265261
}
266262

267263
// get all append fields to include
@@ -288,7 +284,7 @@ protected function addRequestedModelFieldsToQuery()
288284
return;
289285
}
290286

291-
$prependedFields = $prepend_table_name ? $this->prependFieldsWithTableName($modelFields, $modelTableName) : $modelFields;
287+
$prependedFields = $this->prependFieldsWithTableName($modelFields, $modelTableName);
292288

293289
// get rid of any appended fields present
294290
$prependedFields = array_diff(

Diff for: tests/Feature/ApiControllerTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,24 @@ public function it_can_load_api_model_appends(): void
110110
'slug' => 'orange',
111111
]);
112112
}
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+
}
113133
}

0 commit comments

Comments
 (0)