• Reading time ~ 1 min
  • 28.07.2023

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.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297