The Level Up package is a Laravel package introducing gamification into your applications. Users can earn experience points (XP) and gain levels through your application. It also includes a dynamic leaderboard feature.
This package has three concepts that make up the gamification:
- Experience points (XP)
- Levels
- Achievements
Once you import the GiveExperience
trait on your user model:
use LevelUp\Experience\Concerns\GiveExperience;
class User extends Model
{
use GiveExperience;
// ...
}
This model trait unlocks various methods to work with the gamification API:
$user->addPoints(10);
$user->deductPoints(10);
$user->setPoints(10);
$user->getPoints();
You can create levels for your app with the following API:
Level::add(
['level' => 1, 'next_level_experience' => null],
['level' => 2, 'next_level_experience' => 100],
['level' => 3, 'next_level_experience' => 250],
);
And finally, here's how you create achievements
Achievement::create([
'name' => 'Hit Level 20',
'is_secret' => false,
'description' => 'When a User hits Level 20',
'image' => 'storage/app/achievements/level-20.png',
]);
// Unlock an achievement
$achievement = Achievement::find(1);
$user->grantAchievement($achievement);
// Set achievement progress
$user->grantAchievement(
achievement: $achievement,
progress: 50 // 50%
);
You can also define secret achievements hidden from the user until they are unlocked. This package also supports auditing, tracking when a user gains points, levels up, etc.
You can learn more about this package, get full installation instructions, and view the source code on GitHub.