dongwei1895
2014-09-01 10:00
浏览 119
已采纳

从Laravel中的数据透视表获取特定数据部分

I'm doing a website where companies have persons which have scores per event. And the events are ordered by city.

So by example someone can do 4 events, so they get a score for each event.

I'm using a pivot table to save the scores. ( here's a screenshot )

enter image description here

Now i'm loading all persons for a certain event with the following query.

$data['companies'] = Event::find($id)->city->companies;

And i'm showing them using the following structure in my blade file

@foreach($companies as $company)
   {{ $company->name }}
        @foreach($company->persons as $person)
           {{ $person->firstname }}
           {{ $person->eventscore }}
        @endforeach
    @endforeach

The person->eventscore is the link to the pivot table. And it's working but it's giving me the scores of that person for ALL the events.

I only want to see the score for the event that we are watching now ( you can see the id in the url bar, and i marked the id with a red arrow, Now how can i? In my view ask to only show to score for the specific event?

enter image description here

Thank you!

图片转代码服务由CSDN问答提供 功能建议

我正在建立一个网站,公司的人</ strong> 得分< / strong>每事件</ strong>。 按事件按城市排序。 </ p>

所以举个例子,有人可以做4个事件,所以他们得到每个事件的分数。 </ p>

我正在使用数据透视表来保存分数。 (这是截图)</ p>

</ p>

现在我正在使用以下查询加载特定事件的所有人。</ p>

  $ data ['companies'] = Event :: find(  $ id) - &gt; city-&gt;公司; 
 </ code> </ pre> 
 
 

我在我的刀片文件中使用以下结构显示它们</ p> \ n

  @foreach($ companies as $ company)
 {{$ company-&gt; name}} 
 @foreach($ company-&gt; person as $ person)
 {{$ person-  &gt; firstname}} 
 {{$ person-&gt; eventscore}} 
 @endforeach 
 @endforeach 
 </ code> </ pre> 
 
 

person-&gt; eventscore是 链接到数据透视表。 并且它正在工作,但是它给了我所有事件的那个人的分数。</ h2>

我只想查看我们现在正在观看的事件的分数(你可以在 url栏,我用红色箭头标记了id,现在我怎么样?在我的视图中要求仅显示特定事件的分数?</ p>

</ p>

谢谢!</ p> </ div>

2条回答 默认 最新

相关推荐 更多相似问题