내가 아는 유일한 방법은 다형성 관계를 사용하는 것입니다.
모델에 2 개의 필드 (모델 이름은 1, ID 키는 1)를 위치 테이블에 추가해야하며 명명 규칙은 일반적으로 **** able_type 및 *** able_id 입니다 (예 : locationable_type
). 및 locationable_id
. 위치 모델보다
이보다는 당신의 레스토랑 모델
class Restaurant extends Model
{
/**
* Get the restaurant location.
*/
public function location()
{
return $this->morphMany('App\Location', 'locationable');
}
}
이 관계
class Hotel extends Model
{
/**
* Get the hotel location.
*/
public function location()
{
return $this->morphMany('App\Location', 'locationable');
}
}
같은 일을 추가하는 호텔 모델에 비해 다음과 같은 관계
class Location extends Model
{
/**
* Get all of the owning locationable models.
*/
public function locationable()
{
return $this->morphTo();
}
}
를 추가 그것을 사용하면 간단하게 사용할 수 있습니다 :
(210)
$hotel = App\Hotel::find(1);
dd($hotel->location);
자세한 정보는 laravel documentation
행운을 빕니다 :)
이 https://laravel.com/docs/5.2/eloquent-relationships#polymorphic-relations (다형성 [관계] 체크 체크 아웃 할 수 있습니다). – Burak