2014-02-13 49 views
0

我正在社交應用程序前端Angualar,後端laravel和數據庫Mongodb。我有類似的模型:使用關係在單一的Eloquent Query laravel中獲取數據?

Hoots 
----------------- 
- _id 
- content 
- publish_id 

Article 
----------------- 
- _id 
- content 
- publish_id 

Story 
----------------- 
- _id 
- content 
- publish_id 

Publish 
----------------- 
- _id 
- post_id 
- type 
- user_id 

帖子的ID在發佈所屬hoots,文章和故事,其中type表示wheather是叱,文章或故事_id。

我有型號這樣

//Article model 
class Article extends Eloquent { 

    public function getpublish(){ 
    return $this->hasMany('Publish','post_id'); 
    } 
    } 
//Story model 
class Story extends Eloquent { 

    public function get_publish(){ 
    return $this->hasMany('Publish','post_id'); 
    } 
    } 

//Hoots model 
class Hoots extends Eloquent { 

    public function get_publ(){ 
    return $this->hasMany('Publish','post_id'); 
    } 
    } 

//Publish model 
class Publish extends Eloquent { 

    public function getdata(){ 

    return $this->BelongsTo('Hoots','Article','Story','publish_id'); 
    } 
    } 

我使用這個我只能得到與一個模型只即hoots POST_ID相應的數據一起發佈的數據使用

Publish::with('getdata')->where('user_id',Auth::user()->id)->get(); 

。我想從這三個表中得到這個。

我想獲取發佈模型數據及其相應的post_id數據。我如何使用口才能完成這個o單個查詢。

+0

確實很難找出你所要求的。你能否提供你期望的結果表格?\ –

+0

@Gaz_Edge我已經更新了代碼。 Kindly Review.how我可以獲得發佈模型數據以及具有外鍵publish_id的三個不同模型的post_id相應數據嗎? –

回答

1

我想也許你沒有正確地建立你的關係;

//Publish model 
class Publish extends Eloquent { 

    public function hoot(){ 

    return $this->HasMany('Hoots','publish_id'); 
    } 
    } 

    public function article(){ 

    return $this->HasMany('article','publish_id'); 
    } 
    } 

    public function story(){ 

    return $this->HasMany('stort','publish_id'); 
    } 
    } 

Publish::with(array('hoot', 'story', 'article'))->where('user_id',Auth::user()->id)->get(); 
+0

我不是爲什麼,但這個東西不工作,我只是得到Id不是內容?如上所示,我在連接中指定表名。我正在使用的數據庫是mongodb –

+0

查看更新的答案 –

+0

非常棒的人。我正在嘗試同樣的事情,但與屬地。 hasmany做th魅力。謝謝 –

相關問題