Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit 08c0c2f

Browse files
committed
Add withAttributes for additional data to resources
1 parent bf60040 commit 08c0c2f

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/Http/Resources/JsonApiResource.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ public function toArray($request)
5353
return [
5454
$this->merge($this->getResourceIdentifier()),
5555
'attributes' => $this->getAttributes(),
56-
'relationships' => $this->when(
57-
$this->relationships, $this->relationships
58-
),
56+
'relationships' => $this->when($this->relationships, $this->relationships),
5957
];
6058
}
6159

@@ -94,8 +92,22 @@ public function getResourceIdentifier()
9492
*/
9593
protected function getAttributes()
9694
{
97-
return array_filter($this->resource->attributesToArray(), function ($key) {
98-
return !Str::endsWith($key, '_id') && $key !== $this->resource->getKeyName();
99-
}, ARRAY_FILTER_USE_KEY);
95+
return array_filter(
96+
array_merge($this->resource->attributesToArray(), $this->withAttributes()),
97+
function ($value, $key) {
98+
return !Str::endsWith($key, '_id') && $key !== $this->resource->getKeyName() && !is_null($value);
99+
},
100+
ARRAY_FILTER_USE_BOTH
101+
);
102+
}
103+
104+
/**
105+
* Attach additional attributes data.
106+
*
107+
* @return array
108+
*/
109+
protected function withAttributes()
110+
{
111+
return [];
100112
}
101113
}

0 commit comments

Comments
 (0)