66use Ark4ne \JsonApi \Resources \Relationship ;
77use Ark4ne \JsonApi \Traits \HasRelationLoad ;
88use Closure ;
9+ use Illuminate \Contracts \Auth \Access \Gate ;
910use Illuminate \Database \Eloquent \Model ;
1011use Illuminate \Http \Request ;
1112use Illuminate \Http \Resources \MissingValue ;
@@ -25,12 +26,13 @@ abstract class Relation extends Describer
2526
2627 /**
2728 * @param class-string<\Ark4ne\JsonApi\Resources\JsonApiResource|\Ark4ne\JsonApi\Resources\JsonApiCollection> $related
28- * @param string|\Closure|null $relation
29+ * @param string|\Closure|null $relation
2930 */
3031 public function __construct (
31- protected string $ related ,
32+ protected string $ related ,
3233 protected null |string |Closure $ relation
33- ) {
34+ )
35+ {
3436 }
3537
3638 /**
@@ -61,6 +63,24 @@ public function meta(Closure $meta): static
6163 return $ this ;
6264 }
6365
66+ /**
67+ * @param iterable<mixed>|string $abilities Abilities to check
68+ * @param array<mixed> $arguments Arguments to pass to the policy method, the model is always the first argument
69+ * @param string $gateClass Gate class to use, defaults to the default Gate implementation
70+ * @return static
71+ */
72+ public function can (iterable |string $ abilities , array $ arguments = [], string $ gateClass = Gate::class): static
73+ {
74+ return $ this ->when (fn (
75+ Request $ request ,
76+ Model $ model ,
77+ string $ attribute
78+ ) => app ($ gateClass )
79+ ->forUser ($ request ->user ())
80+ ->allows ($ abilities , [$ model , ...$ arguments ])
81+ );
82+ }
83+
6484 /**
6585 * @param bool|null $whenIncluded
6686 * @return $this
@@ -77,35 +97,34 @@ public function whenIncluded(null|bool $whenIncluded = null): static
7797 }
7898
7999 /**
80- * @see \Illuminate\Http\Resources\ConditionallyLoadsAttributes::whenLoaded
81- *
82100 * @param string|null $relation
83101 *
84102 * @return static
103+ * @see \Illuminate\Http\Resources\ConditionallyLoadsAttributes::whenLoaded
85104 */
86- public function whenLoaded (null |string $ relation = null ): self
105+ public function whenLoaded (null |string $ relation = null ): static
87106 {
88107 return $ this ->when (fn (
89108 Request $ request ,
90- Model $ model ,
91- string $ attribute
109+ Model $ model ,
110+ string $ attribute
92111 ): bool => $ model ->relationLoaded ($ relation ?? (is_string ($ this ->relation ) ? $ this ->relation : $ attribute )));
93112 }
94113
95114 /**
96- * @see \Illuminate\Http\Resources\ConditionallyLoadsAttributes::whenPivotLoadedAs
97- *
98- * @param string $table
115+ * @param string $table
99116 * @param string|null $accessor
100117 *
101118 * @return static
119+ * @see \Illuminate\Http\Resources\ConditionallyLoadsAttributes::whenPivotLoadedAs
120+ *
102121 */
103- public function whenPivotLoaded (string $ table , null |string $ accessor = null ): self
122+ public function whenPivotLoaded (string $ table , null |string $ accessor = null ): static
104123 {
105124 return $ this ->when (fn (
106125 Request $ request ,
107- Model $ model ,
108- string $ attribute
126+ Model $ model ,
127+ string $ attribute
109128 ): bool => ($ pivot = $ model ->{$ accessor ?? (is_string ($ this ->relation ) ? $ this ->relation : $ attribute )})
110129 && (
111130 $ pivot instanceof $ table ||
@@ -139,8 +158,8 @@ public function resolveFor(Request $request, mixed $model, string $field): Relat
139158
140159 /**
141160 * @param \Illuminate\Http\Request $request
142- * @param T $model
143- * @param string $field
161+ * @param T $model
162+ * @param string $field
144163 *
145164 * @return mixed
146165 */
0 commit comments