qcod/laravel-gamify

Need help assigning points to a user

zhpeh opened this issue · 6 comments

zhpeh commented

Hi there,

Thanks for sharing this great package!

I'm having some difficulties trying to assign points to user, here's a snippet of my code:

       $attributes = [
            'user_id' => $id,
            'project_id' => request()->project_id,
        ];

        // Creates the project
        $project = Project::create($attributes);

        // Rewards points
        givePoint(new ProjectCreated($project));

However, I'm getting the following error:

payee() method must return a model which will get the points.

Could you advise me on this?

Share your ProjectCreated point class

zhpeh commented

Thanks for your prompt reply. My ProjectCreated class is as follows:

<?php

namespace App\Gamify\Points;

use QCod\Gamify\PointType;

class ProjectCreated extends PointType
{
    /**
     * Number of points
     *
     * @var int
     */
    public $points = 20;

    /**
     * Point constructor
     *
     * @param $subject
     */
    public function __construct($subject)
    {
        $this->subject = $subject;
    }

    /**
     * User who will be receive points
     *
     * @return mixed
     */
    public function payee()
    {
        return $this->getSubject()->user;
    }

}

make sure Project model has a belongsTo on the User model. and try this at last

// Creates the project
$project = Project::create($attributes);

// Rewards points
givePoint(new ProjectCreated($project->fresh()));

Hey, @saqueib,
This is an awsome package, unfortunately i'm having this same issue, which is that calling
Auth::user()->givePoint(new GroupCommentCreated($comment));
results in
payee() method must return a model which will get the points..

The problem is when calling the method givePoint on a user class ( in my case Auth::user() ), it still tries to find the payee using the payee() function of the PointType Class , which is redundant in my opinion.

The expected behaviour in this situation is to give the points to the user on which the givePoint methond has been executed.

zhpeh commented

Hey @saqueib,

Thanks for your reply. I have figured out the issue with my case which is that my Project model has a method called owner() instead of user(). By changing from owner() to user(), it works now.

For the benefit of others who might be facing the same error message, ensure that your model has the user() method as such:

Project Model:

public function user()
{
     return $this->belongsTo(User::class);
}

The expected behaviour in this situation is to give the points to the user on which the givePoint methond has been executed.

I must agreee with you @walidbagh . How can I achieve that, please (even by writing a custom PointType class). Thanks!
#42