1. Go to this page and download the library: Download cjmellor/engageify library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cjmellor / engageify example snippets
use Cjmellor\Engageify\Concerns\HasEngagements;
class BlogPost extends Model
{
use HasEngagements;
// ...
}
use Cjmellor\Engageify\Contracts\EngagementType;
use Cjmellor\Engageify\Contracts\HasWeight;
enum Vote: string implements EngagementType, HasWeight
{
case Up = 'up';
case Down = 'down';
public function weight(): int
{
return match ($this) {
self::Up => 1,
self::Down => -1,
};
}
}
$post->engage(Vote::Up); // stores the derived value 1
$post->engage(Vote::Down); // stores the derived value -1
$post->score(Vote::Up); // total stored weight for upvotes
$post->averageOf(Vote::Up); // mean stored weight for upvotes
use Cjmellor\Engageify\Contracts\EngagementType;
use Cjmellor\Engageify\Contracts\Exclusive;
use Cjmellor\Engageify\Contracts\HasWeight;
enum Vote: string implements EngagementType, Exclusive, HasWeight
{
case Up = 'up';
case Down = 'down';
public function group(): string
{
return 'vote';
}
public function weight(): int
{
return match ($this) {
self::Up => 1,
self::Down => -1,
};
}
}
$post->engage(Vote::Down); // down
$post->engage(Vote::Up); // switches: down removed, up recorded (one transaction)
$post->engage(Vote::Up); // re-recording the active member toggles it off
use Cjmellor\Engageify\Contracts\EngagementType;
use Cjmellor\Engageify\Contracts\Rateable;
enum Rating: string implements EngagementType, Rateable
{
case Stars = 'stars';
public function min(): float
{
return 1.0;
}
public function max(): float
{
return 5.0;
}
public function step(): ?float
{
return 1.0;
}
}
$film->averageRating(Rating::Stars); // mean rating
$film->ratingCount(Rating::Stars); // number of ratings
$film->ratingDistribution(Rating::Stars); // ['5.00' => 12, '4.00' => 3, ...]
$film->bayesianAverage(Rating::Stars); // mean pulled toward the global average for low-count items
// Most-liked posts first
Post::query()->orderByEngagementCount(EngagementTypes::Like)->get();
// Highest net vote score first
Post::query()->orderByScore('vote')->get();
// Hot — net score balanced against age (stored, time-anchored; no cron)
Post::query()->hot()->get();
// Top — items created within a window, ranked by net score ('all' = no window)
Post::query()->top('week')->get();
// Bayesian — honest average that pulls low-count items toward the global mean
Film::query()->orderByBayesian(Rating::Stars)->get();
use Cjmellor\Engageify\Concerns\EngagesWith;
class User extends Authenticatable
{
use EngagesWith;
}
$user->hasEngaged($post, EngagementTypes::Like); // bool
$user->engagementValueFor($post, Vote::Up); // the stored value, or null
$user->ratingFor($film, Rating::Stars); // alias for engagementValueFor
$user->engagements; // every engagement this user authored
$feed = Post::query()->withUserEngagement(EngagementTypes::Like)->get();
$feed->first()->is_engaged; // true/false — no extra query per row
$post->likes(showUsers: true);
use Cjmellor\Engageify\Concerns\HasViews;
class Thread extends Model
{
use HasViews;
}
$thread->recordView(); // counts once per viewer per cooldown window
$thread->viewCount(); // lifetime total
Thread::query()->orderByMostViewed()->get(); // most viewed, all-time
$thread->viewsInLast(7); // views in the last 7 days
Thread::query()->orderByMostViewed('week')->get(); // most viewed this week
public Model $actor,
public Model $engageable,
public EngagementType $type,
public Engagement $engagement,
public Model $actor,
public Model $engageable,
public EngagementType $type,