2015-06-19 68 views
2

爲了改進我的Laravel開發風格,我查看了this文章。所以我爲我的用戶模型創建了一個Interface和Repository。但是這個模型與「主題」模型有關。我也爲這個創建了Interface和Repository。但是我需要在關係中提到什麼?模型/實體本身還是接口/存儲庫?創建存儲庫,在模型關係中引用什麼?

$id = 1; 
$user = User::find($id); 
$new_topics = $user -> topics() -> new(); 

只是我想要執行的代碼的基本示例。那麼我需要參考什麼?這是用戶模型:

/* The Model/Entity */ 
public function topics() 
{ 
    return $this -> hasMany('App\Models\Entities\Topic'); 
} 

/* OR */ 

/* The Interface */ 
public function topics() 
{ 
    return $this -> hasMany('App\Models\Repositories\TopicInterface'); 
} 

回答

0

您需要將模型傳遞給hasOne或其他關係方法,可用於Eloquent ORM。所以,你正在尋找的答案是,

/* The Model/Entity */ 
public function topics() 
{ 
    return $this -> hasMany('App\Models\Entities\Topic'); 
} 

以往模式,你在的hasMany功能speficied什麼東西有延長口才 或關係模式將無法正常工作。

0

您提到的文章解釋瞭如何實現一個使處理實體更優雅的外觀,但它與模型化實體間關係的Laravel的ORM部分無關。因此,在處理ORM時,您必須引用模型類而不是存儲庫。 因此,您的問題的答案是

/* The Model/Entity */ 
public function topics() 
{ 
    return $this -> hasMany('App\Models\Entities\Topic'); 
}