여기에 3 개 관계가 있습니다. 당신은 조직 A가 조직 B 및 관계 유형과 관련이 있다고 말합니다. 대다수의 경우 삼항 관계가 이진 관계로 단순화 될 수 있기 때문에 매우 드문 경우입니다. 귀하의 사례가 단순화 될 수 있는지 여부를 판단하기 위해 데이터 모델을 면밀히 검사해야하지만 여기에는 제 제안이 없다고 가정합니다.
the eloquent docs 특히 아래에서 확인하십시오. 사용자 지정 중간 표 모델 정의. 이 경우 Laravel 5.4 이상이 작동해야합니다.
다음 작업을해야합니다 :
class OrganisationOrganisationLink extends Pivot {
public relationType() {
return $this->belongsTo(RelationType::class); //You need to specify the foreign key correctly as a 2nd parameter
}
}
을 그리고 원래 모델 : 그런 다음
class Organisation extends Model {
public relatedOrganisation() {
return $this->belongsToMany(self::class)->using(OrganisationOrganisationLink::class);
}
}
당신이 예를 들어, 할 수있는이의 실제 사용을 할 때 do :
$organisation = Organisation::with('relatedOrganisation')->first();
echo "Got ".$organisation->name." which relates to "
.$organisation->relatedOrganisation->first()->name
." with relationship type "
$organisation->relatedOrganisation->first()->pivot->relationshipType()->value('name');
물론 내가 생각한 필드는 존재하지 않을 수 있지만 잘하면 당신은 아이디어를 얻을 수 있습니다.