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 6
6
use Illuminate \Database \Eloquent \Model ;
7
7
use Illuminate \Database \Eloquent \Relations \MorphTo ;
8
8
use Tobyz \JsonApiServer \Context ;
9
+ use Tobyz \JsonApiServer \Laravel \Field \ToMany ;
10
+ use Tobyz \JsonApiServer \Laravel \Field \ToOne ;
9
11
use Tobyz \JsonApiServer \Schema \Field \Relationship ;
10
12
11
13
abstract class EloquentBuffer
@@ -56,7 +58,21 @@ public static function load(
56
58
$ modelClass = get_class ($ resource ->newModel ($ context ));
57
59
58
60
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
+ };
60
76
}
61
77
}
62
78
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 2
2
3
3
namespace Tobyz \JsonApiServer \Schema \Field ;
4
4
5
+ use Closure ;
5
6
use Tobyz \JsonApiServer \Context ;
6
7
use Tobyz \JsonApiServer \Exception \BadRequestException ;
7
8
use Tobyz \JsonApiServer \Exception \Sourceable ;
You can’t perform that action at this time.
0 commit comments