4
4
5
5
use Closure ;
6
6
use Illuminate \Database \Eloquent \Builder ;
7
+ use Illuminate \Database \Eloquent \Collection ;
7
8
use Illuminate \Database \Eloquent \Relations \Relation ;
8
9
use Illuminate \Http \Request ;
9
10
use Illuminate \Http \Response ;
11
+ use Illuminate \Pagination \LengthAwarePaginator ;
10
12
use InvalidArgumentException ;
11
13
12
14
abstract class ResourceRelations {
@@ -36,43 +38,41 @@ final public function __construct(
36
38
protected Request $ request ,
37
39
) {}
38
40
39
- final public function handle (Builder $ query , Closure $ next ): void {
41
+ final public function handle (Builder $ query , Closure $ next ): Builder | Collection | LengthAwarePaginator {
40
42
// 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 );
63
47
}
64
48
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
+ );
68
55
}
69
56
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
+ }
76
76
}
77
77
}
78
78
@@ -84,7 +84,7 @@ final public function handle(Builder $query, Closure $next): void {
84
84
// append relation counts to the query
85
85
$ query ->withCount ($ this ->with_count );
86
86
87
- $ next ($ query );
87
+ return $ next ($ query );
88
88
}
89
89
90
90
private function parseWiths (): void {
0 commit comments