Skip to content

Commit 8d7f49c

Browse files
authored
Merge 4b889b0 into 5ba985e
2 parents 5ba985e + 4b889b0 commit 8d7f49c

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/Support/JsonApiTransforms.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function transformRecord($record, array $fields = [], array $include =
4848
}
4949

5050
$data = array_filter([
51-
'type' => $record->getTable(),
51+
'type' => str_slug($record->getTable()),
5252
'id' => $record->id,
5353
'attributes' => array_except($attributes, ['id']),
5454
'relationships' => $relationships,
@@ -71,7 +71,7 @@ protected function transformRecordSimple($record)
7171

7272
return [
7373
'data' => [
74-
'type' => $record->getTable(),
74+
'type' => str_slug($record->getTable()),
7575
'id' => $record->id,
7676
'attributes' => $attributes,
7777
]
@@ -89,7 +89,7 @@ protected function transformRecordIdentifier($record)
8989
{
9090
return [
9191
'data' => [
92-
'type' => $record->getTable(),
92+
'type' => str_slug($record->getTable()),
9393
'id' => $record->id,
9494
]
9595
];
@@ -100,16 +100,25 @@ protected function transformRecordIdentifier($record)
100100
*
101101
* @param Collection|LengthAwarePaginator $records
102102
* @param array $fields
103+
* @param array|null $include
103104
*
104105
* @return array
105106
*/
106-
protected function transformCollection($records, array $fields = [])
107+
protected function transformCollection($records, array $fields = [], array $include = [])
107108
{
108-
$data = $records->map(function ($record) use ($fields) {
109-
return $this->transformRecord($record, $fields)['data'];
110-
})->toArray();
111-
109+
$data = [];
112110
$links = [];
111+
$included = [];
112+
113+
foreach ($records as $record) {
114+
$object = $this->transformRecord($record, $fields, $include);
115+
116+
if (isset($object['included'])) {
117+
$included = array_merge($included, $object['included']);
118+
}
119+
120+
$data[] = $object['data'];
121+
}
113122

114123
if ($records instanceof LengthAwarePaginator) {
115124
$links['first'] = $records->url(1);
@@ -118,7 +127,7 @@ protected function transformCollection($records, array $fields = [])
118127
$links['next'] = $records->nextPageUrl();
119128
}
120129

121-
return array_merge(compact('data'), array_filter(compact('links')));
130+
return array_merge(compact('data'), array_filter(compact('links', 'included')));
122131
}
123132

124133
/**

0 commit comments

Comments
 (0)