我正在使用Laravel来查询具有分数的用户(eventscore表中的行),我想返回ID为3的所有具有该事件分数的用户
是否只能返回在数据透视表中找到的每个用户的分数(请参见下面的结果)
这是我正在使用的代码
$persons = Person::whereHas('eventscore', function($q) {
$q->where('id', 3);
})->with('eventscore')->get();
return $persons;
谢谢!
试试这个:
// Person model
// accessor for easy fetching the score from the pivot model
public function getScoreAttribute()
{
return ($this->pivot && $this->pivot->score)
? $this->pivot->score
: null;
}
// example usage for the event's page (I assume participants is the relation)
$event = Event::with('participants')->find(3);
// somewhere in the view template
@foreach ($event->participants as $participant)
{{ $participant->name }} - score: {{ $participant->score }}
@endforeach
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句