All Projects → gstt → Laravel Achievements

gstt / Laravel Achievements

Licence: mit
Achievements for Laravel 5.3+

Projects that are alternatives of or similar to Laravel Achievements

Pagination
🎁 Laravel 5 Custom Pagination Presenter
Stars: ✭ 119 (-57.35%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Blogetc
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.
Stars: ✭ 198 (-29.03%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Laravel Auto Translate
Automatically translate your language files using a translator service
Stars: ✭ 153 (-45.16%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Laravel Pdf
A Simple package for easily generating PDF documents from HTML. This package is specially for laravel but you can use this without laravel.
Stars: ✭ 79 (-71.68%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Sneaker
An easy way to send emails whenever an exception occurs on server.
Stars: ✭ 223 (-20.07%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Laravel Excel
🚀 Supercharged Excel exports and imports in Laravel
Stars: ✭ 10,417 (+3633.69%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Laravel Page Speed
Package to optimize your site automatically which results in a 35%+ optimization
Stars: ✭ 2,097 (+651.61%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Laravel Email Verification
Laravel package to handle user verification using an activation mail
Stars: ✭ 63 (-77.42%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Hooks
Hooks is a extension system for your Laravel application.
Stars: ✭ 202 (-27.6%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Voyager Frontend
The Missing Front-end for The Missing Laravel Admin 🔥
Stars: ✭ 200 (-28.32%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Laraupdater
Enable Laravel App Self-Update. Allow your Laravel Application to auto-update itself.
Stars: ✭ 75 (-73.12%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Laravel Gitscrum
GitScrum is a Project Management Tool, developed to help entrepreneurs, freelancers, managers, and teams Skyrocket their Productivity with the Agile methodology and Gamification.
Stars: ✭ 2,686 (+862.72%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Laravel Remember Uploads
Laravel Middleware and helper for remembering file uploads during validation redirects
Stars: ✭ 67 (-75.99%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Eye
Eyewitness.io package for Laravel 5 applications
Stars: ✭ 114 (-59.14%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Laravel Api Health
Monitor first and third-party services and get notified when something goes wrong!
Stars: ✭ 65 (-76.7%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Telegram Bot Sdk
🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
Stars: ✭ 2,212 (+692.83%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Orm
A drop-in Doctrine ORM 2 implementation for Laravel 5+ and Lumen
Stars: ✭ 712 (+155.2%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Laravel Settings
Simple Settings package for a laravel application
Stars: ✭ 45 (-83.87%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Voyager Hooks
Hooks system integrated into Voyager.
Stars: ✭ 200 (-28.32%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Auth Tests
Always-current tests for Laravel's authentication system. Curated by the community.
Stars: ✭ 230 (-17.56%)
Mutual labels:  laravel, laravel-package, laravel-5-package

Laravel Achievements Logo

Build Status Total Downloads License

An implementation of an Achievement System in Laravel, inspired by Laravel's Notification system.

Table of Contents

  1. Requirements
  2. Installation
  3. Creating Achievements
  4. Unlocking Achievements
  5. Adding Progress
  6. Retrieving Achievements
  7. Event Listeners
  8. License

Requirements

  • Laravel 5.3 or higher
  • PHP 5.6 or higher

Installation

Default installation is via Composer.

composer require gstt/laravel-achievements

Add the Service Provider to your config/app file in the providers section.

'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, 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

namespace App\Achievements;

use Gstt\Achievements\Achievement;

class UserMadeAPost extends Achievement
{
    /*
     * The achievement name
     */
    public $name = "Post Created";

    /*
     * A small description for the achievement
     */
    public $description = "Congratulations! You have made your first post!";
}

Unlocking Achievements

Achievements can be unlocked by using the Achiever trait.

<?php

namespace App;

use Gstt\Achievements\Achiever;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Achiever;
}

This trait contains the unlock method, that can be used to unlock an Achievement. The unlock method expects an Achievement instance:

use App\Achievements\UserMadeAPost

$user->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.

Adding Progress

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

namespace App\Achievements;

use Gstt\Achievements\Achievement;

class UserMade10Posts extends Achievement
{
    /*
     * The achievement name
     */
    public $name = "10 Posts Created";

    /*
     * A small description for the achievement
     */
    public $description = "Wow! You have already created 10 posts!";
    
    /*
     * The amount of "points" this user need to obtain in order to complete this achievement
     */
    public $points = 10;
}

You may now control the progress by the methods addProgress and removeProgress on the Achiever trait. Both methods expect an Achievement instance and an amount of points to add or remove:

use App\Achievements\UserMade10Posts;

$user->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:

use App\Achievements\FiveConsecutiveSRanks;

if($rank != 'S'){
    $user->resetProgress(new FiveConsecutiveSRanks());
} else {
    $user->addProgress(new FiveConsecutiveSRanks(), 1);
}
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.

$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.

$details = $user->achievementStatus(new UserMade10Posts());

There are also three additional helpers on the Achiever trait: lockedAchievements(), inProgressAchievements() and unlockedAchievements().

Event Listeners

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.

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

namespace App\Achievements;

use Gstt\Achievements\Achievement;

class UserMade50Posts extends Achievement
{
    /*
     * The achievement name
     */
    public $name = "50 Posts Created";

    /*
     * A small description for the achievement
     */
    public $description = "Wow! You have already created 50 posts!";
    
    /*
     * The amount of "points" this user need to obtain in order to complete this achievement
     */
    public $points = 50;
    
    /*
     * Triggers whenever an Achiever makes progress on this achievement
    */
    public function whenProgress($progress)
    {
        
    }
    
    /*
     * Triggers whenever an Achiever unlocks this achievement
    */
    public function whenUnlocked($progress)
    {
        
    }
}

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].