Skip to content

Commit 2bd5358

Browse files
committed
feat: relationship: allow to work with something else than resourceable
1 parent 759f9bc commit 2bd5358

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/Relationship.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class Relationship implements Resourceable
1313
{
1414
public function __construct(
15-
protected Resourceable $resource,
15+
protected $resource,
1616
protected iterable|Closure $links = [],
1717
protected iterable|Closure $meta = []
1818
) {
@@ -49,9 +49,13 @@ public function toArray($request, bool $minimal = false): array
4949
];
5050
}
5151

52-
$included = [];
52+
if (method_exists($resource, 'toArray')) {
53+
$datum = $resource->toArray($request, $minimal);
54+
} else {
55+
$datum = collect($resource)->toArray();
56+
}
5357

54-
$datum = $resource->toArray($request, $minimal);
58+
$included = [];
5559

5660
if ($resource instanceof ResourceCollection) {
5761
foreach ($datum as $value) {
@@ -76,7 +80,7 @@ public function toArray($request, bool $minimal = false): array
7680
return array_filter([
7781
'data' => array_filter($data),
7882
'included' => $included,
79-
'with' => $resource->with($request)
83+
'with' => method_exists($resource, 'with') ? $resource->with($request) : null
8084
]);
8185
}
8286

0 commit comments

Comments
 (0)