cjmellor / level-up

Level-Up is a Laravel package introducing gamification into your applications. Users earn experience points (XP) and levels through interactions, while also unlocking achievements. It promotes engagement, competition, and fun through its dynamic leaderboard feature. Customisable to fit your specific needs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: LeaderboardService has a hardcoded dependency on a "users" table

gjsman opened this issue · comments

What happened?

The LeaderboardService.php has a hard-coded dependency on there being a users table, ignoring the configuration file with ->whereColumn('user_id', 'users.id').

<?php

namespace LevelUp\Experience\Services;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use LevelUp\Experience\Models\Experience;

class LeaderboardService
{
    private mixed $userModel;

    public function __construct()
    {
        $this->userModel = config(key: 'level-up.user.model');
    }

    public function generate(bool $paginate = false, int $limit = null): array|Collection|LengthAwarePaginator
    {
        return $this->userModel::query()
            ->with(relations: ['experience', 'level'])
            ->orderByDesc(
                column: Experience::select('experience_points')
                    ->whereColumn('user_id', 'users.id')
                    ->latest()
            )
            ->take($limit)
            ->when($paginate, fn (Builder $query) => $query->paginate(), fn (Builder $query) => $query->get());
    }
}

How to reproduce the bug

Use a table that isn't named 'users'.

Package Version

latest

PHP Version

8.2.11

Laravel Version

10

Which operating systems does with happen with?

No response

Notes

Two database migrations also have hardcoded references to users, while the rest read from the config.

Hi,

Thanks for bringing these to my attention.

Are you up for doing a PR?

If not I'll get to it.

Thanks.