Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

level-up

> 后端框架
Open source

Level-Up is a Laravel package introducing gamification into your applications. Users earn experience points (XP) and levels through interactions, while also unl

672 stars0 likes0 views
WebsiteGitHub

About

Level-Up is a Laravel package introducing gamification into your applications. Users earn experience points (XP) and levels through interactions, while also unl

?style=for-the-badge&logo=laravel)

This package allows users to gain experience points (XP) and progress through levels by performing actions on your site. It can provide a simple way to track user progress and implement gamification elements into your application

Installation

You can install the package via composer:

composer require cjmellor/level-up

You can publish and run the migrations with:

php artisan vendor:publish --tag="level-up-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="level-up-config"

This is the contents of the published config file:

…

Customizing Table Names

If you're installing into an app that already has tables called experiences, levels, tiers, multipliers, challenges, or any of the package's other defaults, you can rename them via config — no need to patch published migrations.

Option 1 — Apply a single prefix to every package table:

Set an env var (no config publish required):

dotenv
LEVEL_UP_TABLE_PREFIX=levelup_

…or publish the config and edit the prefix line:

bash
php artisan vendor:publish --tag=level-up-config
php
// config/level-up.php
'table_prefix' => 'levelup_',

All package tables now use the prefix: levelup_experiences, levelup_levels, levelup_tiers, levelup_multipliers, levelup_challenges, and so on.

Option 2 — Rename specific tables:

Edit the tables array in the published config:

php
'tables' => [
    'experiences' => 'xp_log',         // renamed
    'levels'      => 'user_tiers',     // renamed
    'tiers'       => 'rank_brackets',  // renamed
    // leave the rest at their defaults
],

Combining both: any value left equal to the default receives the table_prefix; any value you change is taken verbatim and the prefix is NOT applied. This lets you prefix everything but override one or two outliers:

php
'table_prefix' => 'lvl_',
'tables' => [
    'levels' => 'xp_levels',  // → 'xp_levels' (NO prefix)
    // others stay default → all become 'lvl_'
],

Upgrading from v1.x or earlier v2: the previous top-level 'table' config key (used to override only the experiences table) still works as a fallback. New installations should prefer 'tables.experiences' instead.

Usage

Experience Points (XP)

Add the GiveExperience trait to your User model.

php
use LevelUp\Experience\Concerns\GiveExperience;

class User extends Model
{
    use GiveExperience;

    // ...
}

Give XP points to a User

php
$user->addPoints(10);

A new record will be added to the experiences table which stores the Users’ points. If a record already exists, it will be updated instead. All new records will be given a level_id of 1.

[!NOTE] If you didn't set up your Level structure yet, a default Level of 1 will be added to get you started.

Deduct XP points from a User

php
$user->deductPoints(10);

[!NOTE] Both deductPoints and setPoints will throw an exception if the User has no experience record.

Set XP points to a User

For an event where you just want to directly add a certain number of points to a User. Points can only be set if the User has an Experience Model.

php
$user->setPoints(10);

Retrieve a Users’ points

php
$user->getPoints();

Multipliers

Multipliers modify the experience points a User earns. They are managed via the database, so you can create, schedule, and toggle multipliers at runtime — no code deployments needed. This makes them ideal for building admin panels or managing promotional events.

Create a Multiplier

php
use LevelUp\Experience\Models\Multiplier;

Multiplier::create([
    ‘name’ => ‘WrestleMania Weekend’,
    ‘multiplier’ => 5,
    ‘is_active’ => true,
    ‘starts_at’ => ‘2026-04-05 00:00:00’,
    ‘expires_at’ => ‘2026-04-07 23:59:59’,
]);

Active multipliers are automatically applied when a User earns points via addPoints(). Time-based multipliers activate and deactivate based on starts_at and expires_at. Non-time-based multipliers (like "Man UTD Win the League") are toggled via is_active.

Scope Multipliers to Users or Tiers

By default, a multiplier applies to all Users. You can restrict it to specific Users or Tiers:

php
// Scope to one or more tiers (variadic, idempotent)
$multiplier->scopeToTier($premiumTier, $diamondTier);

// Scope to one or more users (variadic, idempotent)
$multiplier->scopeToUser($user);

// Reverse either with unscope helpers
$multiplier->unscopeFromTier($diamondTier);
$multiplier->unscopeFromUser($user);

// Check whether a multiplier has any scopes
$multiplier->isGlobal();  // true if no users or tiers attached

If a multiplier has no scopes, it applies to everyone. If it has scopes, it only applies to Users who match (either directly or via their Tier).

The tiers() and users() relations are standard belongsToMany — you can drop down to attach() / detach() / sync() for full control, but scopeToUser / scopeToTier are the recommended convenience methods because they're idempotent (they use syncWithoutDetaching under the hood, so calling them twice with the same model doesn't create duplicates).

Query Multipliers

php
Multiplier::active()->get();               // Currently active
Multiplier::active()->forUser($user)->get(); // Active for a specific user
Multiplier::scheduled()->get();             // Armed but starts_at is in the future
Multiplier::expired()->get();               // Past their expires_at

Stacking Strategy

When multiple multipliers are active, you can control how they combine via the stack_strategy config:

MULTIPLIER_STACK=compound   # 2x × 5x = 10x (default)
MULTIPLIER_STACK=additive   # 2x + 5x = 7x
MULTIPLIER_STACK=highest    # max(2x, 5x) = 5x

Inline Multipliers

You can also pass a one-off multiplier directly when adding points. This participates in the configured stacking strategy alongside any active DB multipliers:

php
$user->addPoints(
    amount: 10,
    multiplier: 2
);

Events

PointsIncreased - When points are added.

php
public int $pointsAdded,
public int $totalPoints,
public string $type,
public ?string $reason,
public Model $user,
public ?array $multipliers, // Applied multipliers (if any)

MultiplierApplied - When multipliers are applied during point addition.

php
public Model $user,
public Collection $multipliers,   // DB multiplier models that were applied
public int $originalAmount,       // Points before multipliers
public int $finalAmount,          // Points after multipliers
public string $strategy,          // 'compound', 'additive', or 'highest'

PointsDecreased - When points are decreased.

php
public int $pointsDecreasedBy,
public int $totalPoints,
public ?string $reason,
public Model $user,

⬆️ Levelling

[!NOTE] If you add points before setting up your levelling structure, a default Level of 1 will be added to get you started.

Set up your levelling structure

The package has a handy facade to help you create your levels.

php
Level::add(
    ['level' => 1, 'next_level_experience' => null],
    ['level' => 2, 'next_level_experience' => 100],
    ['level' => 3, 'next_level_experience' => 250],
);

Level 1 should always be null for the next_level_experience as it is the default starting point.

As soon as a User gains the correct number of points listed for the next level, they will level-up.

[!TIP] a User gains 50 points, they’ll still be on Level 1, but gets another 50 points, so the User will now move onto Level 2

See how many points until the next level

php
$user->nextLevelAt();

Get the Users’ current Level

php
$user->getLevel();

Level Cap

A level cap sets the maximum level that a user can reach. Once a user reaches the level cap, they will not be able to gain any more levels, even if they continue to earn experience points. The level cap is enabled by default and capped to level 100. These options can be changed in the packages config file at config/level-up.php or by adding them to your .env file.

LEVEL_CAP_ENABLED=
LEVEL_CAP=
LEVEL_CAP_POINTS_CONTINUE

By default, even when a user hits the level cap, they will continue to earn experience points. To freeze this, so points do not increase once the cap is hit, turn on the points_continue option in the config file, or set it in the .env.

Events

UserLevelledUp - When a User levels-up

php
public Model $user,
public int $level

Achievements

This is a feature that allows you to recognise and reward users for completing specific tasks or reaching certain milestones. You can define your own achievements and criteria for earning them. Achievements can be static or have progression. Static meaning the achievement can be earned instantly. Achievements with progression can be earned in increments, like an achievement can only be obtained once the progress is 100% complete.

Creating Achievements

There is no built-in methods for creating achievements, there is just an Achievement model that you can use as normal:

php
Achievement::create([
    'name' => 'Hit Level 20',
    'is_secret' => false,
    'description' => 'When a User hits Level 20',
    'image' => 'storage/app/achievements/level-20.png',
]);

Gain Achievement

To use Achievements in your User model, you must first add the Trait.

php
// App\Models\User.php

use LevelUp\Experience\Concerns\HasAchievements;

class User extends Authenticable
{
	use HasAchievements;
	
	// ...
}

Then you can start using its methods, like to grant a User an Achievement:

php
$achievement = Achievement::find(1);

$user->grantAchievement($achievement);

To retrieve your Achievements:

php
$user->getUserAchievements();

Revoke Achievement

You can revoke an achievement from a user using the revokeAchievement method:

php
$user->revokeAchievement($achievement);

The method will throw an exception if you try to revoke an achievement that the user doesn't have. You can revoke both standard and secret achievements, and it will also remove any associated progress.

When an achievement is revoked, a AchievementRevoked event is dispatched.

Add progress to Achievement

php
$user->grantAchievement(
    achievement: $achievement, 
    progress: 50 // 50%
);

[!NOTE] Achievement progress is capped to 100%

Track an absolute count

Progress is a percentage, so it can't tell you the absolute number behind it — how many games were played, articles read, and so on. Pass an optional count alongside the progress to track that number on the same achievement:

php
$user->grantAchievement(
    achievement: $achievement,
    progress: 50,
    count: 5 // e.g. 5 games played
);

Unlike progress, the count is open-ended — it isn't capped at 100.

Check Achievement Progression

Check at what progression your Achievements are at.

php
$user->achievementsWithProgress()->get();

Check Achievements that have a certain amount of progression:

php
$user->achievementsWithSpecificProgress(25)->get();

Increase Achievement Progression

You can increment the progression of an Achievement up to 100.

php
$user->incrementAchievementProgress(
    achievement: $achievement, 
    amount: 10
);

Pass a count to also increment the achievement's absolute count by that amount:

php
$user->incrementAchievementProgress(
    achievement: $achievement,
    amount: 10,
    count: 1 // one more game played
);

A AchievementProgressionIncreased Event runs on method execution.

Secret Achievements

Secret achievements are achievements that are hidden from users until they are unlocked.

Secret achievements are made secret when created. If you want to make a non-secret Achievement secret, you can

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPachievementsengagmentexperiencegamification

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架