Achievements
Achievement and reputation system for Laravel applications.
Installation
composer require bradietilley/laravel-achievements
Publish the config and migrations:
php artisan vendor:publish --tag=achievements-config
php artisan vendor:publish --tag=achievements-migrations
php artisan migrate
Add the achievements (and optionally reputation) traits to your user model:
use BradieTilley\Achievements\Concerns\HasAchievements;
use BradieTilley\Achievements\Concerns\HasReputation;
use BradieTilley\Achievements\Contracts\EarnsAchievements;
use BradieTilley\Achievements\Contracts\EarnsReputation;
class User extends Authenticatable implements EarnsAchievements, EarnsReputation
{
use HasAchievements;
use HasReputation;
}
Usage
Create an achievement with one or more criteria and the events that should evaluate it:
use BradieTilley\Achievements\Criteria\HasRelationCountCriteria;
use BradieTilley\Achievements\Models\Achievement;
use App\Models\Post;
$achievement = Achievement::create([
'name' => 'Prolific Poster',
'tier' => 'bronze',
'reverseable' => true,
'async' => false,
'criteria' => [
new HasRelationCountCriteria('posts', 10),
],
'events' => [],
]);
$achievement->listenToEloquent(Post::class, 'created');
$achievement->save();
Grant or revoke achievements manually:
$user->giveAchievement('Prolific Poster');
$user->hasAchievement('Prolific Poster'); // true
$user->revokeAchievement('Prolific Poster');
Track reputation points:
$user->giveReputation(10, 'Completed onboarding');
$user->reputation->points; // 10