2014-02-13 2 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가 게시는 고함 소리, 기사 또는 이야기 wheather를 유형 의미 야유, 기사 및 이야기에 _ID에 속하는 : 나는 모델과 같은 것이있다.

는 나는이 내가 단지 만 야유 즉 하나 개의 모델에서 post_id를 해당 데이터와 함께 데이터를 게시 얻을 수있는 사용

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

을 사용하고이

//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'); 
    } 
    } 

같은 모델이있다. 나는이 세 표 모두에서 이것을 원한다.

모델 데이터를 해당 post_id 데이터와 함께 가져오고 싶습니다. 어떻게하면 능변을 사용하여 단일 쿼리를 수행 할 수 있습니까?

+0

정말 열심히 당신이 무엇을 요구하고 있습니다. \ –

+0

@Gaz_Edge 코드를 업데이트했습니다. 친절하게 검토 .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가 아닌 콘텐츠를 gettting 무엇입니까? 위에 나와있는 것처럼 조인 내부에 테이블 이름을 지정하고 있습니다. 데이터베이스를 사용하고 있습니다 mongodb –

+0

업데이트 된 답변보기 –

+0

굉장한 남자. 나는 똑같은 것을 시도했지만 속박했다. hasmany는 매력을 않습니다. 감사 –

관련 문제