Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help assigning points to a user #22

Closed
zhpeh opened this issue Dec 2, 2019 · 6 comments
Closed

Need help assigning points to a user #22

zhpeh opened this issue Dec 2, 2019 · 6 comments

Comments

@zhpeh
Copy link

zhpeh commented Dec 2, 2019

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?

@saqueib
Copy link
Member

saqueib commented Dec 2, 2019

Share your ProjectCreated point class

@zhpeh
Copy link
Author

zhpeh commented Dec 3, 2019

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;
    }

}

@saqueib
Copy link
Member

saqueib commented Dec 5, 2019

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()));

@walidbagh
Copy link

walidbagh commented Feb 10, 2020

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
Copy link
Author

zhpeh commented Feb 14, 2020

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);
}

@zhpeh zhpeh closed this as completed Feb 14, 2020
@jadamec
Copy link

jadamec commented Nov 23, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants