File tree Expand file tree Collapse file tree 5 files changed +55
-1
lines changed
Expand file tree Collapse file tree 5 files changed +55
-1
lines changed Original file line number Diff line number Diff line change 66use Illuminate \Database \Eloquent \Model ;
77use Illuminate \Database \Eloquent \Relations \MorphTo ;
88use Tobyz \JsonApiServer \Context ;
9+ use Tobyz \JsonApiServer \Laravel \Field \ToMany ;
10+ use Tobyz \JsonApiServer \Laravel \Field \ToOne ;
911use Tobyz \JsonApiServer \Schema \Field \Relationship ;
1012
1113abstract class EloquentBuffer
@@ -56,7 +58,21 @@ public static function load(
5658 $ modelClass = get_class ($ resource ->newModel ($ context ));
5759
5860 if ($ resource instanceof EloquentResource && !isset ($ constrain [$ modelClass ])) {
59- $ constrain [$ modelClass ] = fn ($ query ) => $ resource ->scope ($ query , $ context );
61+ $ constrain [$ modelClass ] = function ($ query ) use (
62+ $ resource ,
63+ $ context ,
64+ $ relationship ,
65+ ) {
66+ $ resource ->scope ($ query , $ context );
67+
68+ if (
69+ ($ relationship instanceof ToMany ||
70+ $ relationship instanceof ToOne) &&
71+ $ relationship ->scope
72+ ) {
73+ ($ relationship ->scope )($ query , $ context );
74+ }
75+ };
6076 }
6177 }
6278
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tobyz \JsonApiServer \Laravel \Field \Concerns ;
4+
5+ use Closure ;
6+
7+ trait ScopesRelationship
8+ {
9+ public ?Closure $ scope = null ;
10+
11+ public function scope (?Closure $ scope ): static
12+ {
13+ $ this ->scope = $ scope ;
14+
15+ return $ this ;
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tobyz \JsonApiServer \Laravel \Field ;
4+
5+ use Tobyz \JsonApiServer \Schema \Field \ToMany as BaseToMany ;
6+
7+ class ToMany extends BaseToMany
8+ {
9+ use Concerns \ScopesRelationship;
10+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tobyz \JsonApiServer \Laravel \Field ;
4+
5+ use Tobyz \JsonApiServer \Schema \Field \ToOne as BaseToOne ;
6+
7+ class ToOne extends BaseToOne
8+ {
9+ use Concerns \ScopesRelationship;
10+ }
Original file line number Diff line number Diff line change 22
33namespace Tobyz \JsonApiServer \Schema \Field ;
44
5+ use Closure ;
56use Tobyz \JsonApiServer \Context ;
67use Tobyz \JsonApiServer \Exception \BadRequestException ;
78use Tobyz \JsonApiServer \Exception \Sourceable ;
You can’t perform that action at this time.
0 commit comments