I have 3 tables: emotions, users, and emotion_user. The emotion_user table has an extra column in it called emotion_score. I can query the emotion table through users and get a list of emotion names, but how would I get the emotion_score out of the pivot table in the controller?
To get the emotion names I can use this:
$emotionScore = User::find(Auth::user()->id)->emotion()->get()->pluck('name');
But I need the scores. The two models are:
User:
public function emotion()
{
return $this->belongsToMany(Emotion::class)->withPivot('emotion_score');
}
Emotion:
public function user()
{
return $this->belongsToMany(User::class)->withPivot('emotion_score');
}
Further to this, I wanted to get a sum of all the scores for a particular user so I was trying something like this:
$score = 0;
foreach($request->emotions as $emotion) {
$emotionScore = User::find(Auth::user()->id)->emotion()->get();
$score += $emotionScore;
}
but I was stuck on getting the emotion scores