2017-08-17 22 views
1

我得到了TagAttendee雄辯的模型,它們處於多對多的關係。樞軸表還有兩個屬性 - value_intvalue_string。我Attendee模式是這樣的:Laravel 5 - 根據型號和相關型號獲得特定的多對多關係ID

class Attendee extends Model 
{ 
    public $timestamps = false; 

    protected $fillable = [ 
     'event_id' 
    ]; 

    public function tags() { 
     return $this->belongsToMany('App\Models\Tag', 'attendee_tag', 'attendee_id', 'tag_id') 
      ->withPivot(['value_string', 'value_int']); 
    } 

    public function scoreTagValue($tag_id) { 
     return $this->tags->where('tag_id', '=', $tag_id)->first(); 
    } 

} 

我想是獲得基於Attendee模型和可變tag_id樞紐值,所以我寫scoreTagValue功能,但它總是返回null,我不知道爲什麼:(我打電話這樣說: $attendee->scoreTagValue($tag_id)感謝您的幫助:)

+0

沒有,變量值,我在尋找,被稱爲比分:) – user3216673

回答

0

您需要訪問的關係,而不是財產:

public function scoreTagValue($tag_id) { 
    return $this->tags()->where('tag_id', '=', $tag_id)->first(); 
} 

此外,according to the docswithPivot()用不了數組,因此:

->withPivot('value_string', 'value_int'); 
+0

其實我不認爲這是正確的 - 做它有幫助嗎? –

+0

好吧,現在我得到標籤,但沒有透視數據:) – user3216673

+0

哦! :-)好的,更新了我的答案。 –