Skip to content

Commit 40afed2

Browse files
committed
FIX: Attribute with should always be parsed
1 parent 7c7c6ad commit 40afed2

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

src/ResourceRelations.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use Closure;
66
use Illuminate\Database\Eloquent\Builder;
7+
use Illuminate\Database\Eloquent\Collection;
78
use Illuminate\Database\Eloquent\Relations\Relation;
89
use Illuminate\Http\Request;
910
use Illuminate\Http\Response;
11+
use Illuminate\Pagination\LengthAwarePaginator;
1012
use InvalidArgumentException;
1113

1214
abstract class ResourceRelations {
@@ -36,43 +38,41 @@ final public function __construct(
3638
protected Request $request,
3739
) {}
3840

39-
final public function handle(Builder $query, Closure $next): void {
41+
final public function handle(Builder $query, Closure $next): Builder | Collection | LengthAwarePaginator {
4042
// check if query param wasn't defined and just return
41-
if (null === $with = $this->request->query('with')) {
42-
$next($query);
43-
44-
return;
45-
}
46-
47-
// convert to array if it is a coma separated string
48-
if (is_string($with) && str_contains($with, ',')) {
49-
$with = explode(',', $with);
50-
}
51-
52-
// must be an array
53-
if ( !is_array($with)) {
54-
throw new InvalidArgumentException(
55-
message: 'Parameter "with" must be an array.',
56-
code: Response::HTTP_BAD_REQUEST,
57-
);
58-
}
59-
60-
foreach ($this->allowed_relations as $mapping => $relation_name) {
61-
if (is_int($mapping)) {
62-
$mapping = $relation_name;
43+
if (null !== $with = $this->request->query('with')) {
44+
// convert to array if it is a coma separated string
45+
if (is_string($with) && str_contains($with, ',')) {
46+
$with = explode(',', $with);
6347
}
6448

65-
// ignore relation if not specified in params
66-
if ( !in_array($mapping, $with, true)) {
67-
continue;
49+
// must be an array
50+
if ( !is_array($with)) {
51+
throw new InvalidArgumentException(
52+
message: 'Parameter "with" must be an array.',
53+
code: Response::HTTP_BAD_REQUEST,
54+
);
6855
}
6956

70-
// check if a method with the relation name exists
71-
if (method_exists($this, $method = explode('.', $mapping, 2)[0])) {
72-
// redirect relation to the custom method implementation
73-
$this->with[$relation_name] = fn(Relation $relation) => $this->$method($relation);
74-
} else {
75-
$this->with[] = $relation_name;
57+
foreach ($this->allowed_relations as $mapping => $relation_name) {
58+
if (is_int($mapping)) {
59+
$mapping = $relation_name;
60+
}
61+
62+
// ignore relation if not specified in params
63+
if ( !in_array($mapping, $with, true)) {
64+
continue;
65+
}
66+
67+
foreach ((array) $relation_name as $relationship_name) {
68+
// check if a method with the relation name exists
69+
if (method_exists($this, $method = explode('.', $mapping, 2)[0])) {
70+
// redirect relation to the custom method implementation
71+
$this->with[$relation_name] = fn(Relation $relation) => $this->$method($relation);
72+
} else {
73+
$this->with[] = $relationship_name;
74+
}
75+
}
7676
}
7777
}
7878

@@ -84,7 +84,7 @@ final public function handle(Builder $query, Closure $next): void {
8484
// append relation counts to the query
8585
$query->withCount($this->with_count);
8686

87-
$next($query);
87+
return $next($query);
8888
}
8989

9090
private function parseWiths(): void {

0 commit comments

Comments
 (0)