Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gstt/laravel-achievements
Achievements for Laravel 5.3+
https://github.com/gstt/laravel-achievements
achievements gamification laravel laravel-5-package laravel-package
Last synced: 26 days ago
JSON representation
Achievements for Laravel 5.3+
- Host: GitHub
- URL: https://github.com/gstt/laravel-achievements
- Owner: gstt
- License: mit
- Created: 2017-02-18T21:18:54.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-05-28T23:18:21.000Z (over 3 years ago)
- Last Synced: 2024-09-28T20:03:11.449Z (about 1 month ago)
- Topics: achievements, gamification, laravel, laravel-5-package, laravel-package
- Language: PHP
- Size: 2.76 MB
- Stars: 297
- Watchers: 14
- Forks: 38
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
An implementation of an Achievement System in Laravel, inspired by Laravel's Notification system.
## Table of Contents
1. [Requirements](#requirements)
2. [Installation](#installation)
3. [Creating Achievements](#creating)
4. [Unlocking Achievements](#unlocking)
5. [Adding Progress](#progress)
6. [Retrieving Achievements](#retrieving)
7. [Event Listeners](#listening)
8. [License](#license)- Laravel 5.3 or higher
- PHP 5.6 or higherDefault installation is via [Composer](https://getcomposer.org/).
```
composer require gstt/laravel-achievements
```Add the Service Provider to your `config/app` file in the `providers` section.
```php
'providers' => [
...
Gstt\Achievements\AchievementsServiceProvider::class,
```Backup your database and run the migrations in order to setup the required tables on the database.
```
php artisan migrate
```## Creating Achievements
Similar to Laravel's implementation of [Notifications](https://laravel.com/docs/5.4/notifications), each Achievement is
represented by a single class (typically stored in the `app\Achievements` directory.) This directory will be created
automatically for you when you run the `make:achievement` command.```
php artisan make:achievement UserMadeAPost
```
This command will put a fresh Achievement in your `app/Achievements` directory with only has two properties defined:
`name` and `description`. You should change the default values for these properties to something that better explains
what the Achievement is and how to unlock it. When you're done, it should look like this:```php
Unlocking Achievements
Achievements can be unlocked by using the `Achiever` trait.```php
unlock(new UserMadeAPost());
```
Remember that you're not restricted to the `User` class. You may add the `Achiever` trait to any entity that could
unlock Achievements.Instead of directly unlocking an achievement, you can add a progress to it. For example, you may have an achievement
`UserMade10Posts` and you want to keep track of how the user is progressing on this Achievement.In order to do that, you must set an additional parameter on your `UserMade10Posts` class, called `$points`:
```php
addProgress(new UserMade10Posts(), 1); // Adds 1 point of progress to the UserMade10Posts achievement
```In addition, you can use the methods `resetProgress` to set the progress back to 0 and `setProgress` to set it to a
specified amount of points:```php
use App\Achievements\FiveConsecutiveSRanks;if($rank != 'S'){
$user->resetProgress(new FiveConsecutiveSRanks());
} else {
$user->addProgress(new FiveConsecutiveSRanks(), 1);
}
``````php
use App\Achievements\Have1000GoldOnTheBag;$user->setProgress(new Have100GoldOnTheBag(), $user->amountOfGoldOnTheBag);
```Once an Achievement reach the defined amount of points, it will be automatically unlocked.
## Retrieving Achievements
The `Achiever` trait also adds a convenient relationship to the entity implementing it: `achievements()`. You can use it
to retrieve progress for all achievements the entity has interacted with. Since `achievements()` is a relationship, you
can use it as a QueryBuilder to filter data even further.```php
$achievements = $user->achievements;
$unlocked_today = $user->achievements()->where('unlocked_at', '>=', Carbon::yesterday())->get();
```You can also search for a specific achievement using the `achievementStatus()` method.
```php
$details = $user->achievementStatus(new UserMade10Posts());
```There are also three additional helpers on the `Achiever` trait: `lockedAchievements()`, `inProgressAchievements()` and `unlockedAchievements()`.
### Listening to all Achievements
Laravel Achievements provides two events that can be listened to in order to provide "Achievement Unlocked" messages or similar. Both events receive the instance of `AchievementProgress` that triggered them.The `Gstt\Achievements\Event\Progress` event triggers whenever an Achiever makes progress, but doesn't unlock an Achievement. The `Gstt\Achievements\Event\Unlocked` event triggers whenever an Achiever actually unlocks an achievement.
Details on how to listen to those events are explained on [Laravel's Event documentation](https://laravel.com/docs/5.3/events).### Listening to specific Achievements
The event listeners mentioned above triggers for all Achievements. If you would like to add an event listener for only a specific Achievement, you can do so by implementing the methods `whenUnlocked` or `whenProgress` on the `Achievement` class.
```php
LicenseLaravel Achievements is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).