diff --git a/README.md b/README.md index b14ddda..b498eca 100644 --- a/README.md +++ b/README.md @@ -101,11 +101,17 @@ foreach($likers as $user) { // all $user->likes()->count(); +// short way +$user->totalLikes; + // with type $user->likes()->withType(Post::class)->count(); // likers count $post->likers()->count(); + +// short way +$post->totalLikers ``` List with `*_count` attribute: diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 46a93e1..b4372b9 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,14 @@ - - - - src/ - - + + ./tests/ + + + src/ + + diff --git a/phpunit.xml.dist.bak b/phpunit.xml.dist.bak new file mode 100644 index 0000000..46a93e1 --- /dev/null +++ b/phpunit.xml.dist.bak @@ -0,0 +1,13 @@ + + + + + src/ + + + + + ./tests/ + + + diff --git a/src/Traits/Likeable.php b/src/Traits/Likeable.php index 36e7c8b..d63d7aa 100644 --- a/src/Traits/Likeable.php +++ b/src/Traits/Likeable.php @@ -2,6 +2,7 @@ namespace Overtrue\LaravelLike\Traits; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; trait Likeable @@ -32,4 +33,11 @@ public function likers(): \Illuminate\Database\Eloquent\Relations\BelongsToMany ) ->where('likeable_type', $this->getMorphClass()); } + + protected function totalLikers(): Attribute + { + return Attribute::get(function () { + return $this->likers()->count() ?? 0; + }); + } } diff --git a/src/Traits/Liker.php b/src/Traits/Liker.php index 846ab25..00402cd 100644 --- a/src/Traits/Liker.php +++ b/src/Traits/Liker.php @@ -3,6 +3,7 @@ namespace Overtrue\LaravelLike\Traits; use Illuminate\Contracts\Pagination\Paginator; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Pagination\AbstractCursorPaginator; @@ -136,4 +137,11 @@ public function attachLikeStatus(&$likeables, callable $resolver = null) throw new \InvalidArgumentException('Invalid argument type.'); } } + + protected function totalLikes(): Attribute + { + return Attribute::make(get: function ($value) { + return $this->likes()->count() ?? 0; + }); + } } diff --git a/tests/FeatureTest.php b/tests/FeatureTest.php index af817d0..bf12ae9 100644 --- a/tests/FeatureTest.php +++ b/tests/FeatureTest.php @@ -80,6 +80,7 @@ public function test_aggregations() $this->assertSame(4, $user->likes()->count()); $this->assertSame(2, $user->likes()->withType(Book::class)->count()); + $this->assertSame(4, $user->totalLikes); } public function test_like_same_model() @@ -106,6 +107,7 @@ public function test_object_likers() $user2->like($post); $this->assertCount(2, $post->likers); + $this->assertEquals(2, $post->totalLikers); $this->assertSame('overtrue', $post->likers[0]['name']); $this->assertSame('allen', $post->likers[1]['name']); diff --git a/tests/User.php b/tests/User.php index 7a2c070..8d05662 100644 --- a/tests/User.php +++ b/tests/User.php @@ -8,8 +8,8 @@ class User extends Model { - use Liker; use Likeable; + use Liker; protected $fillable = ['name']; }