User clap/unclap behaviour for Laravel.

3.3.0 2024-03-18 14:48 UTC

README

User clap/unclap behaviour for Laravel.

Latest Stable Version Total Downloads Latest Unstable Version License

Introduction

It let people express how they feel about the model(documentation/subject/topic).

Installation

Requirements

Instructions

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

Usage

Setup Clapper

use Illuminate\Database\Eloquent\Model;
use LaravelInteraction\Clap\Concerns\Clapper;

class User extends Model
{
    use Clapper;
}

Setup Ratable

use Illuminate\Database\Eloquent\Model;
use LaravelInteraction\Clap\Concerns\Clappable;

class Channel extends Model
{
    use Clappable;
}

Clapr

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();

Ratable

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"
});

Events

License

Laravel Clap is an open-sourced software licensed under the MIT license.