33namespace Tobyz \JsonApiServer \Laravel \Filter ;
44
55use Tobyz \JsonApiServer \Context ;
6- use Tobyz \JsonApiServer \Exception \BadRequestException ;
76use Tobyz \JsonApiServer \Schema \Filter ;
87
98class WhereBelongsTo extends Filter
109{
10+ use SupportsOperators;
11+
12+ public const SUPPORTED_OPERATORS = ['eq ' , 'in ' , 'ne ' , 'notin ' , 'null ' , 'notnull ' ];
13+
1114 protected ?string $ relationship = null ;
1215
1316 public static function make (string $ name ): static
@@ -25,16 +28,12 @@ public function relationship(?string $relationship): static
2528 public function apply (object $ query , array |string $ value , Context $ context ): void
2629 {
2730 $ relationship = $ query ->getModel ()->{$ this ->relationship ?: $ this ->name }();
31+ $ column = $ relationship ->getQualifiedForeignKeyName ();
2832
29- if (!array_is_list ($ values = (array ) $ value )) {
30- throw (new BadRequestException ('filter value must be list ' ))->setSource ([
31- 'parameter ' => "[ $ this ->name ] " ,
32- ]);
33- }
34-
35- $ query ->whereIn (
36- $ relationship ->getQualifiedForeignKeyName (),
37- array_merge (...array_map (fn ($ v ) => explode (', ' , $ v ), $ values )),
38- );
33+ Where::make ($ this ->name )
34+ ->column ($ column )
35+ ->operators ($ this ->operators )
36+ ->commaSeparated ()
37+ ->apply ($ query , $ value , $ context );
3938 }
4039}
0 commit comments