1
我旁邊型號:Laravel 5.3得到belongsToMany和計數旋轉
class Polling extends Model
{
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function participants()
{
return $this->belongsToMany(Participant::class, 'participant_poll', 'poll_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function results()
{
return $this->belongsToMany(Participant::class, 'poll_results', 'poll_id');
}
}
class Participant extends Model
{
public function polls()
{
return $this->belongsToMany(Polling::class);
}
public function results()
{
return $this->belongsToMany(Polling::class);
}
}
poll_results - 透視表具有的結構:ID,poll_id,participant_id。 我需要查看下錶:
№|participant.name|Count vote| 1|Mike |15 | 2|................|10 | ..............................
計票獲得的數據透視表poll_results。 請幫忙寫下查詢。
$poll = Polling::first();
$poll->participants()->get();
這回participan ts計數(獲得參與者表)。列數投票 - 需要從poll_results表中獲取。 –