qcod / laravel-gamify

Gamify your Laravel app with Reputation Points & Achievements Badges support

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

CyrilBlankaert opened this issue · comments

Hello,

When I try to do this :

           $create_post = Posts::create(array(
               'content' => $get_content,
               'user_id' => Auth::user()->id
           ));

           givePoint(new PostCreated($create_post));

It returns an error : payee() method must return a model which will get the points.

How to resolve this ? Thanks ! :)

I've updated my code, but it doesn't work yet

           $get_content = $request->input('content');
           $user_id = Auth::user()->id;

           $post = $user->posts()->create(array(
               'content' => $get_content,
               'user_id' => $user_id
           ));

           givePoint(new PostCreated($post));

Please share your PostCreated point class

I didn't change the base of the PostCreated Class, as mentioned in the readme file.

<?php

namespace App\Gamify\Points;

use QCod\Gamify\PointType;

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

$this->getSubject()->user return "null" value

$this return :

PostCreated {#996 ▼ +points: 20 #payee: "user" #subject: Posts {#962 ▼ #table: "posts" #fillable: array:4 [▶] #connection: "mysql" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: true #attributes: array:5 [▶] #original: array:5 [▶] #changes: [] #casts: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #hidden: [] #visible: [] #guarded: array:1 [▶] } }

It looks like you don't have user relation setup on Post model

class Post extends Model
    ...

   public function user() {
        return $this->belongsTo('App\User');
   }
}

please check and it should work

It works, i already have a function with users but called "Users" and not "user"
Thanks for your help !