PHP code example of particle-academy / laravel-fun-lab
1. Go to this page and download the library: Download particle-academy/laravel-fun-lab library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
particle-academy / laravel-fun-lab example snippets
use LaravelFunLab\Traits\Awardable;
class User extends Authenticatable
{
use Awardable;
}
// Works with any model!
class Team extends Model
{
use Awardable;
}
// Award XP to a specific GamedMetric
LFL::award('combat-xp')
->to($user)
->amount(50)
->because('defeated boss')
->save();
// Check the profile
$profile = $user->getProfile();
echo $profile->total_xp; // Total XP across all metrics
// Create an achievement
LFL::setup(a: 'achievement', with: ['slug' => 'first-login', 'name' => 'First Login', 'description' => 'Welcome!', 'icon' => 'star']);
// Grant the achievement
LFL::grant('first-login')
->to($user)
->because('completed onboarding')
->save();
// Check if user has an achievement
if ($user->hasAchievement('first-login')) {
// ...
}
// Check if user has reached a specific level in a metric
if (LFL::hasLevel($user, 3, metric: 'combat-xp')) {
echo "You're a Warrior!";
}
// Check level in a metric group
if (LFL::hasLevel($user, 10, group: 'overall-power')) {
echo "You've reached Overall Power Level 10!";
}
// Create a group
LFL::setup(a: 'metric-level-group', with: ['slug' => 'overall-power', 'name' => 'Overall Power']);
// Add metrics to the group with weights
LFL::setup(a: 'metric-level-group-metric', with: ['group' => 'overall-power', 'metric' => 'combat-xp', 'weight' => 1.0]);
LFL::setup(a: 'metric-level-group-metric', with: ['group' => 'overall-power', 'metric' => 'crafting-xp', 'weight' => 0.5]);
// Define levels for the group
LFL::setup(a: 'metric-level-group-level', with: ['group' => 'overall-power', 'level' => 1, 'xp' => 0, 'name' => 'Beginner']);
LFL::setup(a: 'metric-level-group-level', with: ['group' => 'overall-power', 'level' => 10, 'xp' => 5000, 'name' => 'Expert']);
// Get top users by XP
$leaders = LFL::leaderboard()
->for(User::class)
->by('xp')
->take(10);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.