jeudi 21 mai 2020

How to make polymorphism

I have tried and i am still seeing an error in Pivot please suggest what to do I have been stuck for two days debuting this code

namespace App;

use Illuminate\Database\Eloquent\Model;

class Answer extends Model { protected $fillable = ['body', 'user_id'];

public function question()
{
    return $this->belongsTo(Question::class);
}

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

public function getBodyHtmlAttribute()
{
    return \Parsedown::instance()->text($this->body);
}

public static function boot()
{
    parent::boot();

    static::created(function ($answer) {
        $answer->question->increment('answers_count');                     
    });        

    static::deleted(function ($answer) {            
        $answer->question->decrement('answers_count');            
    });
}

public function getCreatedDateAttribute()
{
    return $this->created_at->diffForHumans();
}

public function getStatusAttribute()
{
    return $this->isBest() ? 'vote-accepted' : '';
}

public function getIsBestAttribute()
{
    return $this->isBest();
}

public function isBest()
{
    return $this->id === $this->question->best_answer_id;
}

public function votes()
{
    return $this->morphedToMany(User::class, 'votable');
}

public function upVotes()
{
    return $this->votes()->wherePivot('vote', 1);
}

public function downVotes()
{
    return $this->votes()->wherePivot('vote', -1);
}

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire