2121 */
2222trait Followable
2323{
24+ /**
25+ * @return bool
26+ */
27+ public function needsToApproveFollowRequests ()
28+ {
29+ return false ;
30+ }
31+
2432 /**
2533 * @param \Illuminate\Database\Eloquent\Model|int $user
34+ *
35+ * @return array
2636 */
2737 public function follow ($ user )
2838 {
29- $ this ->followings ()->attach ($ user );
39+ $ isPending = $ user ->needsToApproveFollowRequests () ?: false ;
40+
41+ $ this ->followings ()->attach ($ user , [
42+ 'accepted_at ' => $ isPending ? null : now ()
43+ ]);
44+
45+ return ['pending ' => $ isPending ];
3046 }
3147
3248 /**
3349 * @param \Illuminate\Database\Eloquent\Model|int $user
34- *
35- * @return int
3650 */
3751 public function unfollow ($ user )
3852 {
39- return $ this ->followings ()->detach ($ user );
53+ $ this ->followings ()->detach ($ user );
4054 }
4155
4256 /**
4357 * @param \Illuminate\Database\Eloquent\Model|int $user
4458 *
45- * @return array|array[]
4659 */
4760 public function toggleFollow ($ user )
4861 {
49- return $ this ->followings ()->toggle ($ user );
62+ $ this ->isFollowing ($ user ) ? $ this ->unfollow ($ user ) : $ this ->follow ($ user );
63+ }
64+
65+ /**
66+ * @param \Illuminate\Database\Eloquent\Model|int $user
67+ */
68+ public function rejectFollowRequestFrom ($ user )
69+ {
70+ $ this ->followers ()->detach ($ user );
71+ }
72+
73+ /**
74+ * @param \Illuminate\Database\Eloquent\Model|int $user
75+ */
76+ public function acceptFollowRequestFrom ($ user )
77+ {
78+ $ this ->followers ()->updateExistingPivot ($ user , ['accepted_at ' => now ()]);
79+ }
80+
81+ /**
82+ * @param \Illuminate\Database\Eloquent\Model|int $user
83+ */
84+ public function hasRequestedToFollow (Model $ user ): bool
85+ {
86+ if ($ user instanceof Model) {
87+ $ user = $ user ->getKey ();
88+ }
89+
90+ /* @var \Illuminate\Database\Eloquent\Model $this */
91+ if ($ this ->relationLoaded ('followings ' )) {
92+ return $ this ->followings
93+ ->whereNull ('pivot.accepted_at ' )
94+ ->contains ($ user );
95+ }
96+
97+ return $ this ->followings ()
98+ ->wherePivot ('accepted_at ' , null )
99+ ->where ($ this ->getQualifiedKeyName (), $ user )
100+ ->exists ();
50101 }
51102
52103 /**
@@ -62,10 +113,15 @@ public function isFollowing($user)
62113
63114 /* @var \Illuminate\Database\Eloquent\Model $this */
64115 if ($ this ->relationLoaded ('followings ' )) {
65- return $ this ->followings ->contains ($ user );
116+ return $ this ->followings
117+ ->whereNotNull ('pivot.accepted_at ' )
118+ ->contains ($ user );
66119 }
67120
68- return $ this ->followings ()->where ($ this ->getQualifiedKeyName (), $ user )->exists ();
121+ return $ this ->followings ()
122+ ->wherePivot ('accepted_at ' , '!= ' , null )
123+ ->where ($ this ->getQualifiedKeyName (), $ user )
124+ ->exists ();
69125 }
70126
71127 /**
@@ -81,10 +137,15 @@ public function isFollowedBy($user)
81137
82138 /* @var \Illuminate\Database\Eloquent\Model $this */
83139 if ($ this ->relationLoaded ('followers ' )) {
84- return $ this ->followers ->contains ($ user );
140+ return $ this ->followers
141+ ->whereNotNull ('pivot.accepted_at ' )
142+ ->contains ($ user );
85143 }
86144
87- return $ this ->followers ()->where ($ this ->getQualifiedKeyName (), $ user )->exists ();
145+ return $ this ->followers ()
146+ ->wherePivot ('accepted_at ' , '!= ' , null )
147+ ->where ($ this ->getQualifiedKeyName (), $ user )
148+ ->exists ();
88149 }
89150
90151 /**
@@ -109,7 +170,7 @@ public function followers()
109170 \config ('follow.relation_table ' , 'user_follower ' ),
110171 'following_id ' ,
111172 'follower_id '
112- )->withTimestamps ()->using (UserFollower::class);
173+ )->withPivot ( ' accepted_at ' )-> withTimestamps ()->using (UserFollower::class);
113174 }
114175
115176 /**
@@ -123,6 +184,6 @@ public function followings()
123184 \config ('follow.relation_table ' , 'user_follower ' ),
124185 'follower_id ' ,
125186 'following_id '
126- )->withTimestamps ()->using (UserFollower::class);
187+ )->withPivot ( ' accepted_at ' )-> withTimestamps ()->using (UserFollower::class);
127188 }
128189}
0 commit comments