2015-04-12 70 views
0

今天我遇到了一個奇怪的問題。 我有一個非常複雜的數據庫,有很多關係。 在我的模型,我得到athe以下功能:開啓模型屬性不起作用 - laravel 4

public function resource(){ 
    dd($this->getAttributes()); 
    switch($this->content_type){ 
     case Content::TYPE_TASKLIST: 
      return $this->belongsTo('Tasklist', 'content_id'); 
     case Content::TYPE_FEEDBACK: 
      return $this->belongsTo('Feedback', 'content_id'); 
     case Content::TYPE_SURVEY: 
      return $this->belongsTo('Survey', 'content_id'); 
     case Content::TYPE_MEMO: 
      return $this->belongsTo('Memo', 'content_id'); 
     case Content::TYPE_GEAR: 
      return $this->belongsTo('Gear', 'content_id'); 
    } 
    return $this->belongsTo('Resource', 'content_id'); 
} 

此功能必須改變的關係,並給我一個好得到正確的信息。 我檢查了千次列的名稱,但我無法使開關工作。

$ this-> content_type始終爲空,因此它不會更改關係。 我嘗試了很多。正如你所看到的,我dd()模型屬性,但它只返回一個空數組。

我不明白爲什麼這個功能不起作用。所有其他realtions我得到,直到這些différentskind od資源是好的。我得到了關於資源ID和內容類型的緊密信息,但根本不起作用。

有人知道爲什麼嗎?

謝謝。

回答

0

我認爲你試圖實現多態關係和Laravel有一個優雅的方式來做到這一點。詳情請參閱http://laravel.com/docs/5.0/eloquent#polymorphic-relations

此外,請確保您有一個裝載數據的模型實例,以便能夠填充屬性。否則,就像你所說的那樣,它將是空白的。