Io Chizna
Io Chizna

Reputation: 11

laravel call to undefined method app\models\User:id()

I have been trying to create a point system, so after much effort i am getting this error which i could not figure out how to solve it because this is my first time working so deep

I have checked code but couldn't pinpoint the error

call to undefined app\models\User:id()

point model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Point extends Model
{
    use HasFactory;
    const TABLE = 'points';
    protected $table = self::TABLE;

    protected $fillable = [
      'id', 'amount', 'message', 'current_points'
    ];

    public function pointable(): MorphTo
    {
        return $this->morphTo();
    }

    public function getCurrentPoints(Model $pointable)
    {
        $currentPoints = Point::where('pointable_id', $pointable->id())
        ->where('pointable_type', $pointable->getMorphClass())
        ->orderBy('created_at', 'desc')
        ->pluck('current_points')->first();

        if($currentPoints){
            $currentPoints = 0;
        }

        return $currentPoints;
    }

    public function addAwards(Model $pointable, $amount, $message)
    {
        $award = new Static();
        $award->amount = $amount;
        $award->current_points = $this->getCurrentPoints($pointable) + $amount;
        $award->message = $message

        $pointable->awards()->save($award);

        return $award;
    }
}


pointable model

<?php

namespace App\Models;

interface pointable
{
    public function awards();

    public function countAwards();

    public function addPoints($amount, $message);
}

hasPoints Traits


<?php

namespace App\Traits;

use App\Models\Point;

trait HasPoints
{
    public function awards($amount = null)
    {
       return $this->morphMany(Point::class, 'pointable')
       ->orderBy('created_at', 'desc')
       ->take($amount);
    }

    public function countAwards()
    {
        return $this->awards()->count();
    }

    public function currentPoints()
    {
        return (new Point())->getCurrentPoints($this);

    }

    public function addPoints($amount, $message)
    {
        return (new Point())->addAwards($this, $amount, $message);
    }
}

AwardPointLItener

?php

namespace App\Listeners;


use App\Events\ReplyWasCreated;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class AwardPointForNewReply
{
    public function handle(ReplyWasCreated $event)
    {
        $amount = config('points.rewards.new_reply');
        $message = 'User Created A New Reply';

        $author = $event->reply->user;
        $author->addPoints($amount, $message);
    }
}

ReplyEvent


<?php

namespace App\Events;

use App\Models\Reply as Replyers;
use Illuminate\Queue\SerializesModels;

class ReplyWasCreated
{
    use SerializesModels;

    public $reply;

    public function __construct(Replyers $reply)
    {
        $this->reply = $reply;
    }

}

livewire reply componet

use Livewire\Component;
use App\Models\Reply as Replys;

class Reply extends Component
{

    public $thread;
    public $username;
    public $reply_text;
    public $replyCommentId = NULL;

    protected $rules = [
        'reply_text' => 'required'
    ];

    public function mount(Thread $thread)
    {
         $this->thread = $thread;
    }


    public function render()
    {

        $replys = Replys::whereNull('parent_id')
            ->with('replies')
            ->with('user')
            ->where('thread_id', $this->thread->id)->paginate()->withQueryString();
            return view('livewire.thread.reply',[
                'replys' => $replys,
            ]);
    }

    public function save_reply()
    {
        $this->validate();

       $replyevent = Replys::create([
            'thread_id' => $this->thread->id,
            'user_id' => auth()->user()->id,
            'reply_text' => $this->reply_text,
            'parent_id' => $this->replyCommentId
        ]);

        event(new ReplyWasCreated($replyevent));

        // $this->username = '';
        $this->reply_text = '';
        $this->replyCommentId = NULL;
    }

    public function deleteReply($id)
    {
        $reply = Replys::FindOrFail($id);
        $reply->delete();
    }

    public function replys($replyId)
    {
        $this->replyCommentId = $replyId;
    }

}

Upvotes: 0

Views: 3495

Answers (1)

IGP
IGP

Reputation: 15786

To get the id of a model, you simply access its id property. There is no id() method.

$currentPoints = Point::where('pointable_id', $pointable->id)

Upvotes: 1

Related Questions