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]: Attempt to read property "status" on null at GiveExperience.php:103

theveloperspl opened this issue · comments

What happened?

I am trying to integrate this package with my application.
However I am not able to pass through registering an user as package causes an error:
[2023-08-24 01:23:49] local.ERROR: Attempt to read property "status" on null {"userId":1,"exception":"[object] (ErrorException(code: 0): Attempt to read property "status" on null at C:\Users\username\Desktop\application_name\script\vendor\cjmellor\level-up\src\Concerns\GiveExperience.php:103)

How to reproduce the bug

I have added GiveExperience trait to Used model as instructed.
I have added custom name and reward columns into levels table.
I have seeded test levels:

$levelsArray = [
            [
                'name' => 'Rookie',
                'required_turnover' => null,
                'reward' => null
            ],
            [
                'name' => 'Seeker',
                'required_turnover' => '200000',
                'reward' => '5000'
            ],
        ];

        foreach ($levelsArray as $index => $level) {
            $levelRecord = new Level();
            $levelRecord->name = $level['name'];
            $levelRecord->level = $index+1;
            $levelRecord->next_level_experience = $level['required_turnover'];
            $levelRecord->reward = $level['reward'];
            $levelRecord->save();
        }

Then I am trying to seed first user

        $user = new User();
        $user->username = config('administrator.username');
        $user->email = config('administrator.email');
        $user->password = config('administrator.password');
        $user->status = UserStatus::active;
        $user->level_id = 1;
        $user->save();

        $user->addPoints(amount: 1);

        $userService = new UserService();
        $userService->createRequiredUsersTables($user->id);

And script is killed with error I have posted above.
What is causing this issue?
Same error happens when I add levels using package provided Level model method on "stock" table.

Package Version

^0.0.6

PHP Version

8.1

Laravel Version

10.2

Which operating systems does with happen with?

Windows

Notes

No response

Same issue happens when I try to seed clear data without any reference to this package.
Basically all operations on User model stopped working.

        $user = new User();
        $user->username = config('administrator.username');
        $user->email = config('administrator.email');
        $user->password = config('administrator.password');
        $user->status = UserStatus::active;
        //$user->level_id = 1;
        $user->save();

        //$user->addPoints(amount: 1);

//        $userService = new UserService();
//        $userService->createRequiredUsersTables($user->id);

I don't think this is a bug with the package (at first glance)

Does adding ->value to the UserStatus enum help at all?

Try adding points after the User is created

User::factory()->createOne();

$user = User::first();

$user->addPoints(1);

Okay, I have found issue, it looks like you are right and it is not package related.
Another package we use on user model overrides getlevel() method with its own and that causes problems.
For future reference package is: toponepercent/baum