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!

  • 写回答

2条回答 默认 最新

  • donglu6303 2017-03-22 10:12
    关注

    There need to a little change in your query.. See the below example.

    class Person extends Eloquent {
        public function events()
        {
            return $this->belongsToMany('Event', 'person_event', 'event_id', 'person_id')->withPivot('id','score');
        }
    }
    
    class Event extends Eloquent {
        public function persons()
        {
            return $this->belongsToMany('Person', 'person_event', 'person_id', 'event_id')->withPivot('id','score');
        }
    }
    

    Now to loading all persons for a certain event, query is ..

    $event_id = 1;
    $persons = Person::with('events')->whereHas('events', function($q) use ($event_id){$q->where('event_id',$event_id);})->get();
    

    In view...

    @foreach($persons->events as $event)
        {{ $event->pivot->score}}
    @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • donglian4879 2014-09-01 12:57
    关注

    You need to specify in your model or more specially in your relation which pivot fields you want.

    Here is just an example which you can do

    function events(){
    
    return $this->belongsToMany('Events', 'person_events', 'person_id', 'event_id')->withPivot('score');
    
    }
    

    Now you can access this score calling pivot as follows.

    @foreach($persons->events as $event)
    
        {{ $event->pivot->score}}
    
    @endforeach
    

    The above foreach loop is just iterating through all person events and getting the score of that specific event from pivot table.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 unity 绘画方面的问题
  • ¥15 FTP 明明给了权限但是还是550 Permission denied问题
  • ¥20 Java的kafka错误unknowHostException
  • ¥20 gbase 8a没有lisense,需要获取一个lisense
  • ¥15 前端的3d饼图不知道用啥框架做的
  • ¥15 算法问题 斐波那契数 解答
  • ¥15 VS2019 SPY++ 获取句柄操作
  • ¥15 Facebook 获取广告
  • ¥15 PID算法的输出结果如何转换成pwm
  • ¥15 java文本解密算法