@@ -23,23 +23,26 @@ abstract class EloquentBuffer
23
23
{
24
24
private static $ buffer = [];
25
25
26
- public static function add (Model $ model , string $ relation ): void
26
+ public static function add (Model $ model , string $ relationName ): void
27
27
{
28
- static ::$ buffer [get_class ($ model )][$ relation ][] = $ model ;
28
+ static ::$ buffer [get_class ($ model )][$ relationName ][] = $ model ;
29
29
}
30
30
31
- public static function load (Model $ model , string $ relation , Relationship $ relationship , Context $ context ): void
31
+ public static function load (Model $ model , string $ relationName , Relationship $ relationship , Context $ context ): void
32
32
{
33
- if (! $ models = static ::$ buffer [get_class ($ model )][$ relation ] ?? null ) {
33
+ if (! $ models = static ::$ buffer [get_class ($ model )][$ relationName ] ?? null ) {
34
34
return ;
35
35
}
36
36
37
37
Collection::make ($ models )->loadMissing ([
38
- $ relation => function ($ query ) use ($ model , $ relation , $ relationship , $ context ) {
39
- // As we're loading the relationship, we need to scope the query
40
- // using the scopes defined in the related API resources. We
38
+ $ relationName => function ($ relation ) use ($ model , $ relationName , $ relationship , $ context ) {
39
+ $ query = $ relation ->getQuery ();
40
+
41
+ // When loading the relationship, we need to scope the query
42
+ // using the scopes defined in the related API resource – there
43
+ // may be multiple if this is a polymorphic relationship. We
41
44
// start by getting the resource types this relationship
42
- // could contain.
45
+ // could possibly contain.
43
46
$ resourceTypes = $ context ->getApi ()->getResourceTypes ();
44
47
45
48
if ($ type = $ relationship ->getType ()) {
@@ -50,33 +53,34 @@ public static function load(Model $model, string $relation, Relationship $relati
50
53
}
51
54
}
52
55
56
+ // Now, construct a map of model class names -> scoping
57
+ // functions. This will be provided to the MorphTo::constrain
58
+ // method in order to apply type-specific scoping.
53
59
$ constrain = [];
54
60
55
61
foreach ($ resourceTypes as $ resourceType ) {
56
62
if ($ model = $ resourceType ->getAdapter ()->model ()) {
57
63
$ constrain [get_class ($ model )] = function ($ query ) use ($ resourceType , $ context ) {
58
- run_callbacks (
59
- $ resourceType ->getSchema ()->getListeners ('scope ' ),
60
- [$ query , $ context ]
61
- );
64
+ $ resourceType ->applyScopes ($ query , $ context );
62
65
};
63
66
}
64
67
}
65
68
66
- if ($ query instanceof MorphTo) {
67
- $ query ->constrain ($ constrain );
69
+ if ($ relation instanceof MorphTo) {
70
+ $ relation ->constrain ($ constrain );
68
71
} else {
69
- reset ($ constrain )($ query-> getQuery () );
72
+ reset ($ constrain )($ query );
70
73
}
71
74
72
- // Also apply relationship scopes to the query.
75
+ // Also apply any local scopes that have been defined on this
76
+ // relationship.
73
77
run_callbacks (
74
78
$ relationship ->getListeners ('scope ' ),
75
- [$ query-> getQuery () , $ context ]
79
+ [$ query , $ context ]
76
80
);
77
81
}
78
82
]);
79
83
80
- static ::$ buffer [get_class ($ model )][$ relation ] = [];
84
+ static ::$ buffer [get_class ($ model )][$ relationName ] = [];
81
85
}
82
86
}
0 commit comments