33namespace Overtrue \LaravelFollow ;
44
55use Illuminate \Contracts \Pagination \Paginator ;
6+ use Illuminate \Database \Eloquent \Builder ;
67use Illuminate \Database \Eloquent \Model ;
8+ use Illuminate \Pagination \CursorPaginator ;
79use Illuminate \Pagination \LengthAwarePaginator ;
8- use Illuminate \Support \Collection ;
9- use Overtrue \ LaravelFavorite \ Traits \ Favoriteable ;
10+ use Illuminate \Support \Enumerable ;
11+ use Illuminate \ Support \ LazyCollection ;
1012
1113/**
1214 * @property \Illuminate\Database\Eloquent\Collection $followings
@@ -29,6 +31,7 @@ public function needsToApproveFollowRequests(): bool
2931 */
3032 public function follow ($ user ): array
3133 {
34+ /** @var \Illuminate\Database\Eloquent\Model|\Overtrue\LaravelFollow\Followable $user */
3235 $ isPending = $ user ->needsToApproveFollowRequests () ?: false ;
3336
3437 $ this ->followings ()->attach ($ user , [
@@ -80,7 +83,6 @@ public function hasRequestedToFollow($user): bool
8083 $ user = $ user ->getKey ();
8184 }
8285
83- /* @var \Illuminate\Database\Eloquent\Model $this */
8486 if ($ this ->relationLoaded ('followings ' )) {
8587 return $ this ->followings
8688 ->where ('pivot.accepted_at ' , '=== ' , null )
@@ -102,7 +104,6 @@ public function isFollowing($user): bool
102104 $ user = $ user ->getKey ();
103105 }
104106
105- /* @var \Illuminate\Database\Eloquent\Model $this */
106107 if ($ this ->relationLoaded ('followings ' )) {
107108 return $ this ->followings
108109 ->where ('pivot.accepted_at ' , '!== ' , null )
@@ -124,7 +125,6 @@ public function isFollowedBy($user): bool
124125 $ user = $ user ->getKey ();
125126 }
126127
127- /* @var \Illuminate\Database\Eloquent\Model $this */
128128 if ($ this ->relationLoaded ('followers ' )) {
129129 return $ this ->followers
130130 ->where ('pivot.accepted_at ' , '!== ' , null )
@@ -163,7 +163,6 @@ public function scopeOrderByFollowersCountAsc($query)
163163
164164 public function followers (): \Illuminate \Database \Eloquent \Relations \BelongsToMany
165165 {
166- /* @var \Illuminate\Database\Eloquent\Model $this */
167166 return $ this ->belongsToMany (
168167 __CLASS__ ,
169168 \config ('follow.relation_table ' , 'user_follower ' ),
@@ -174,7 +173,6 @@ public function followers(): \Illuminate\Database\Eloquent\Relations\BelongsToMa
174173
175174 public function followings (): \Illuminate \Database \Eloquent \Relations \BelongsToMany
176175 {
177- /* @var \Illuminate\Database\Eloquent\Model $this */
178176 return $ this ->belongsToMany (
179177 __CLASS__ ,
180178 \config ('follow.relation_table ' , 'user_follower ' ),
@@ -183,6 +181,26 @@ public function followings(): \Illuminate\Database\Eloquent\Relations\BelongsToM
183181 )->withPivot ('accepted_at ' )->withTimestamps ()->using (UserFollower::class);
184182 }
185183
184+ public function scopeApprovedFollowers (Builder $ query ): \Illuminate \Database \Eloquent \Relations \BelongsToMany
185+ {
186+ return $ this ->followers ()->wherePivotNotNull ('accepted_at ' );
187+ }
188+
189+ public function scopeNotApprovedFollowers (Builder $ query ): \Illuminate \Database \Eloquent \Relations \BelongsToMany
190+ {
191+ return $ this ->followers ()->wherePivotNull ('accepted_at ' );
192+ }
193+
194+ public function scopeApprovedFollowings (Builder $ query ): \Illuminate \Database \Eloquent \Relations \BelongsToMany
195+ {
196+ return $ this ->followings ()->wherePivotNotNull ('accepted_at ' );
197+ }
198+
199+ public function scopeNotApprovedFollowings (Builder $ query ): \Illuminate \Database \Eloquent \Relations \BelongsToMany
200+ {
201+ return $ this ->followings ()->wherePivotNull ('accepted_at ' );
202+ }
203+
186204 public function attachFollowStatus ($ followables , callable $ resolver = null )
187205 {
188206 $ returnFirst = false ;
@@ -196,14 +214,18 @@ public function attachFollowStatus($followables, callable $resolver = null)
196214 $ followables = $ followables ->getCollection ();
197215 break ;
198216 case $ followables instanceof Paginator:
217+ case $ followables instanceof CursorPaginator:
199218 $ followables = \collect ($ followables ->items ());
200219 break ;
220+ case $ followables instanceof LazyCollection:
221+ $ followables = \collect (\iterator_to_array ($ followables ->getIterator ()));
222+ break ;
201223 case \is_array ($ followables ):
202224 $ followables = \collect ($ followables );
203225 break ;
204226 }
205227
206- \abort_if (!($ followables instanceof Collection ), 422 , 'Invalid $followables type. ' );
228+ \abort_if (!($ followables instanceof Enumerable ), 422 , 'Invalid $followables type. ' );
207229
208230 $ followed = UserFollower::where ('follower_id ' , $ this ->getKey ())->get ();
209231
0 commit comments