Skip to content

Commit

Permalink
Merge pull request #64 from lahirulhr/master
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue authored Sep 7, 2023
2 parents 1911675 + fc2847d commit 60511df
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 7 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage/>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
13 changes: 13 additions & 0 deletions phpunit.xml.dist.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
8 changes: 8 additions & 0 deletions src/Traits/Likeable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Overtrue\LaravelLike\Traits;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;

trait Likeable
Expand Down Expand Up @@ -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;
});
}
}
8 changes: 8 additions & 0 deletions src/Traits/Liker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});
}
}
2 changes: 2 additions & 0 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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']);

Expand Down
2 changes: 1 addition & 1 deletion tests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

class User extends Model
{
use Liker;
use Likeable;
use Liker;

protected $fillable = ['name'];
}

0 comments on commit 60511df

Please sign in to comment.