2016-12-06 1 views
0

그래서 기본적으로 4 개의 테이블이 있습니다.Laravel 4 테이블과 웅변적인 관계

표 1 : Fylker (카운티) 표 2 : Kommuner (지방 자치 단체) 표 3 : Poststed (우편) 표 4 : Postnumre (우편)

나는 현재 Laravel 5.3을 사용하고 있는데 데이터베이스가 있습니다 설정 및 시드. 이제 4 가지 모델을 사용해야합니까? 모든 테이블은 이전과 관계가 있습니다.

내가 원하는 것은 Fylker에서 행을 가져온 다음 Fylke와 관련된 Kommuner를 얻기 위해 관계를 사용하고, 다른 관계를 사용하여 모든 Poststeder와 다른 관계를 가져 와서 모든 Postnumre를 얻는 것입니다.

$fylke -> kommuner -> poststeder -> postnumre; 

어떻게 이런 짓을 했을까

이 같은

뭔가 내가 달성하고자하는 무엇인가? Laravel에서 enter image description here

답변

0

enter image description here

enter image description here

enter image description here

는 모든 테이블에 대한 모델을 사용하는 것이 좋습니다입니다.

public function postnumres() { 
    return $this->hasMany(Postnumre::class); 
} 

그리고 당신은 모든 얻을 수 있습니다 :

public function poststeds() { 
    return $this->hasMany(Poststed::class); 
} 

Poststed 모델에서 :

public function kommuners(){ 
    return hasMany(Kommuner::class); 
} 

public function poststeds(){ 
    $kommuners = $this->kommuners; 
    $poststeds = array(); 
    foreach ($kommuners as $kommuner) { 
     $poststeds[] = $kommuner->poststeds; 
    } 
} 

public function postnumres() { 
    $poststeds = $this->poststeds; 
    $postnumres = array(); 
    foreach ($poststeds as $poststed) { 
     $postnumres[] = $poststed->postnumres; 
    } 
} 

Kommuner 모델에서 :

Fylker 모델에서 :이처럼 problev를 해결할 수 있습니다 Postnumres ~ Fylke은 다음과 같습니다. $fylke->postnumres