2017-05-05 2 views
-1

모델을 통해 세 번째 테이블에서 데이터를 가져 오는 방법은 무엇입니까?중간 테이블 Laravel에서 데이터를 가져 오는 방법?

모델이 PrototypePrototypeField입니다. PrototypeField

id | name 

: Prototype

id | name 

테이블에서 이름을 얻는 방법 :

prototype_id | field_id 

또한 필드의 이름이 포함 된 테이블 Field있다 Field 모델 'Protot YPE 'PrototypeField로와 Prototype 상대가 여기서

Prototype.id = PrototypeField.prototype_id 

그래서, Prototype 캔 하나 또는 여러 PrototypeField 있습니다.

+0

는 설정 귀하의 모델에 관계를 가지고 있습니까? –

+0

이 경우에 관계 설정 방법을 설명 할 수 있습니까? – Garaman

답변

0

이 예와 관련되어야하는 필드에 지정 그 안에 관계가 있어야 프로토 모델 예를 들어,

public function fields() 
{ 
    return $this->belongsToMany('App\Fields'); // Change to location of fields model 
} 

밭 모델은 또한 관련되는 프로토 타입을 지정하는 관계를 포함한다을 :

public function prototypes() 
{ 
    return $this->belongsToMany('App\Prototypes'); // Change to location of prototype model 
} 

당신이 다음을 설정 한 후에는 다음 프로토 타입 사용에 속하는 필드를 선택할 수 있습니다 :

Prototype::first()->fields; //This selects the first prototype and gets the associated fields. 

의 역은 다음과 같습니다

Fields::first()->prototypes //This selects the first field and get associated prototypes. 

당신은 Laravel 모델의 설명서를 읽어 정보를 많이 찾을 수 있습니다 - https://laravel.com/docs/5.4/eloquent-relationships

관련 문제