User clap/unclap behaviour for Laravel.
It let people express how they feel about the model(documentation/subject/topic).
Require Laravel Clap using Composer.
composer require laravel-interaction/clap
Publish configuration and migrations
php artisan vendor:publish --tag=clap-config
php artisan vendor:publish --tag=clap-migrations
Run database migrations.
php artisan migrate
use Illuminate\Database\Eloquent\Model;
use LaravelInteraction\Clap\Concerns\Clapper;
class User extends Model
{
use Clapper;
}
use Illuminate\Database\Eloquent\Model;
use LaravelInteraction\Clap\Concerns\Clappable;
class Channel extends Model
{
use Clappable;
}
use LaravelInteraction\Clap\Tests\Models\Channel;
/** @var \LaravelInteraction\Clap\Tests\Models\User $user */
/** @var \LaravelInteraction\Clap\Tests\Models\Channel $channel */
// Clap to Ratable
$user->clap($channel);
// clap is only allowed to be called once
$user->clapOnce($channel);
$user->unclap($channel);
// Compare Ratable
$user->hasClapped($channel);
$user->hasNotClapped($channel);
// Get clapped info
$user->clapperApplause()->count();
// with type
$user->clapperApplause()->withType(Channel::class)->count();
// get clapped channels
Channel::query()->whereClappedBy($user)->get();
// get channels doesnt clapped by user
Channel::query()->whereNotClappedBy($user)->get();
use LaravelInteraction\Clap\Tests\Models\User;
use LaravelInteraction\Clap\Tests\Models\Channel;
/** @var \LaravelInteraction\Clap\Tests\Models\User $user */
/** @var \LaravelInteraction\Clap\Tests\Models\Channel $channel */
// Compare Clapper
$channel->isClappedBy($user);
$channel->isNotClappedBy($user);
// Get clappers info
$channel->clappers->each(function (User $user){
echo $user->getKey();
});
$channel->loadClappersCount();
$channels = Channel::query()->withClappersCount()->get();
$channels->each(function (Channel $channel){
echo $channel->clappers()->count(); // 1100
echo $channel->clappers_count; // "1100"
echo $channel->clappersCount(); // 1100
echo $channel->clappersCountForHumans(); // "1.1K"
});
Event | Fired |
---|---|
LaravelInteraction\Clap\Events\Clapped |
When an object get clapped. |
LaravelInteraction\Clap\Events\Unclapped |
When an object get unclapped. |
Laravel Clap is an open-sourced software licensed under the MIT license.