3
3
use Closure ;
4
4
use Illuminate \Cache \TaggableStore ;
5
5
use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
6
+ use Illuminate \Database \Eloquent \Relations \Pivot ;
7
+ use Illuminate \Support \Collection ;
8
+ use Illuminate \Database \Eloquent \Relations \Relation ;
6
9
7
10
class Builder extends EloquentBuilder
8
11
{
@@ -12,24 +15,27 @@ protected function eagerLoadRelation(array $models, $name, Closure $constraints)
12
15
$ relation ->addEagerConstraints ($ models );
13
16
$ constraints ($ relation );
14
17
18
+ return $ relation ->match (
19
+ $ relation ->initRelation ($ models , $ name ),
20
+ $ this ->cacheResults ($ relation , $ models ),
21
+ $ name
22
+ );
23
+ }
24
+
25
+ protected function cacheResults (Relation $ relation , array $ models ) : Collection
26
+ {
15
27
$ parentIds = implode ('_ ' , collect ($ models )->pluck ('id ' )->toArray ());
16
28
$ parentName = str_slug (get_class ($ relation ->getParent ()));
17
29
$ childName = str_slug (get_class ($ relation ->getRelated ()));
18
- // $cache = cache();
19
- //
20
- // if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
21
- // $cache->tags([$parentName, $childName]);
22
- // }
30
+ $ cache = cache ();
23
31
24
- $ results = cache ()->tags ([$ parentName , $ childName ])
32
+ if (is_subclass_of ($ cache ->getStore (), TaggableStore::class)) {
33
+ $ cache ->tags ([$ parentName , $ childName ]);
34
+ }
35
+
36
+ return cache ()->tags ([$ parentName , $ childName ])
25
37
->rememberForever ("{$ parentName }_ {$ parentIds }- {$ childName }s " , function () use ($ relation ) {
26
38
return $ relation ->getEager ();
27
39
});
28
-
29
- return $ relation ->match (
30
- $ relation ->initRelation ($ models , $ name ),
31
- $ results ,
32
- $ name
33
- );
34
40
}
35
41
}
0 commit comments