Skip to content

Commit 2386dd5

Browse files
committed
Fixed #161.
1 parent 793e8bc commit 2386dd5

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/Followable.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,17 @@ public function attachFollowStatus($followables, callable $resolver = null)
190190

191191
\abort_if(!($followables instanceof Collection), 422, 'Invalid $followables type.');
192192

193-
$followed = $this->followings()->wherePivot('accepted_at', '!=', null)->pluck('following_id');
193+
$followed = UserFollower::where('follower_id', $this->getKey())->get();
194194

195195
$followables->map(function ($followable) use ($followed, $resolver) {
196196
$resolver = $resolver ?? fn ($m) => $m;
197197
$followable = $resolver($followable);
198198

199199
if ($followable && \in_array(Followable::class, \class_uses($followable))) {
200-
$followable->setAttribute('has_followed', $followed->contains($followable->getKey()));
200+
$item = $followed->where('following_id', $followable->getKey())->first();
201+
$followable->setAttribute('has_followed', !!$item);
202+
$followable->setAttribute('followed_at', $item ? $item->created_at : null);
203+
$followable->setAttribute('follow_accepted_at', $item ? $item->accepted_at : null);
201204
}
202205
});
203206

tests/FeatureTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests;
44

5+
use Illuminate\Support\Carbon;
56
use Illuminate\Support\Facades\DB;
67
use Illuminate\Support\Facades\Event;
78
use Overtrue\LaravelFollow\Events\Followed;
@@ -207,9 +208,12 @@ function () use ($user1, $users) {
207208

208209
$this->assertFalse($users[0]->has_followed);
209210
$this->assertTrue($users[1]->has_followed);
211+
$this->assertInstanceOf(Carbon::class, $users[1]->followed_at);
212+
210213
$this->assertTrue($users[2]->has_followed);
211214
$this->assertTrue($users[3]->has_followed);
212215

216+
213217
// with custom resolver
214218
$posts = \collect(['author' => $user2], ['author' => $user3], ['author' => $user4]);
215219
$user1->attachFollowStatus($posts, fn ($post) => $post['author']);

0 commit comments

Comments
 (0)