From 619aa06eb92a089eedfdeeaa92553fef7dfddc90 Mon Sep 17 00:00:00 2001 From: LaHiRu Date: Tue, 5 Sep 2023 13:54:39 +0530 Subject: [PATCH 1/4] totalLikes attribute --- README.md | 3 +++ src/Traits/Liker.php | 9 +++++++++ tests/FeatureTest.php | 1 + 3 files changed, 13 insertions(+) diff --git a/README.md b/README.md index b14ddda..f4f317f 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,9 @@ foreach($likers as $user) { // all $user->likes()->count(); +// short version +$user->totalLikes; + // with type $user->likes()->withType(Post::class)->count(); diff --git a/src/Traits/Liker.php b/src/Traits/Liker.php index 846ab25..fb69549 100644 --- a/src/Traits/Liker.php +++ b/src/Traits/Liker.php @@ -10,6 +10,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\LazyCollection; use Overtrue\LaravelLike\Like; +use Illuminate\Database\Eloquent\Casts\Attribute; trait Liker { @@ -136,4 +137,12 @@ 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..4368eaf 100644 --- a/tests/FeatureTest.php +++ b/tests/FeatureTest.php @@ -79,6 +79,7 @@ public function test_aggregations() $user->like($book2); $this->assertSame(4, $user->likes()->count()); + $this->assertSame(4, $user->totalLikes); $this->assertSame(2, $user->likes()->withType(Book::class)->count()); } From 5a082325888b1c6f385cc0e7f494ccbacea5e695 Mon Sep 17 00:00:00 2001 From: LaHiRu Date: Tue, 5 Sep 2023 13:58:13 +0530 Subject: [PATCH 2/4] phpunit.xml migration --- phpunit.xml.dist | 13 +++++++------ phpunit.xml.dist.bak | 13 +++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 phpunit.xml.dist.bak 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/ + + + From 29595040f504a6c39d33e38008ec8846dd54a241 Mon Sep 17 00:00:00 2001 From: LaHiRu Date: Tue, 5 Sep 2023 13:59:04 +0530 Subject: [PATCH 3/4] formatting --- src/Traits/Liker.php | 5 ++--- tests/User.php | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Traits/Liker.php b/src/Traits/Liker.php index fb69549..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; @@ -10,7 +11,6 @@ use Illuminate\Support\Collection; use Illuminate\Support\LazyCollection; use Overtrue\LaravelLike\Like; -use Illuminate\Database\Eloquent\Casts\Attribute; trait Liker { @@ -137,12 +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/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']; } From fc2847d889e6147672f3867808e40459fc814da3 Mon Sep 17 00:00:00 2001 From: LaHiRu Date: Tue, 5 Sep 2023 14:28:46 +0530 Subject: [PATCH 4/4] totalLikers attribute --- README.md | 5 ++++- src/Traits/Likeable.php | 8 ++++++++ tests/FeatureTest.php | 3 ++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f4f317f..b498eca 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ foreach($likers as $user) { // all $user->likes()->count(); -// short version +// short way $user->totalLikes; // with type @@ -109,6 +109,9 @@ $user->likes()->withType(Post::class)->count(); // likers count $post->likers()->count(); + +// short way +$post->totalLikers ``` List with `*_count` attribute: 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/tests/FeatureTest.php b/tests/FeatureTest.php index 4368eaf..bf12ae9 100644 --- a/tests/FeatureTest.php +++ b/tests/FeatureTest.php @@ -79,8 +79,8 @@ public function test_aggregations() $user->like($book2); $this->assertSame(4, $user->likes()->count()); - $this->assertSame(4, $user->totalLikes); $this->assertSame(2, $user->likes()->withType(Book::class)->count()); + $this->assertSame(4, $user->totalLikes); } public function test_like_same_model() @@ -107,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']);