mardi 9 mai 2017

Laravel count using eloquent between two tables

I have a Answer Model

 public function upvote_answers()
    {
        return $this->hasMany('App\UpvoteAnswer');
    }

And I also have a UpvoteAnswer Model

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

I have a table which is named upvote_answers which has column such as id, user_id, answer_id(a different answers table, this id is referencing to that table), and upvote(which is boolean 1 or 0)

I want a count of upvote and downvote for individual answers so i have done like this

$answer = UpvoteAnswer::find($answerid) ;
$counting = $answer->upvote_answers->count();
echo $counting;

i was trying to echo total count for one answer but this doesn't seem to work, I am trying to do eager load , how can i build this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire