Laravel 웅변형성이 하나하나요?
-
21-12-2019 - |
문제
나가려고 설정한 다형성이나 관계(이 다형성의 상당 has_one 및 belongs_to).요 Address
모델,그리고 여러 다른 모델에는 내가 갖고 싶어 하나 주소입니다.그러나 내가 혼동해서 표현의 문서.나 morphMany
방법,그러나 나의 질문은:사용해야 합니까 morphMany
더라도 나는 그것을 하나의 주소 또는 무언가가 있음 morphOne
방법이 사용해야?
편집:나 Address
모델는 이 필드는,단지를 시각화하는 데 도움이 되도록:
Schema::create('addresses', function ($table)
{
$table->increments('id');
$table->string('street');
$table->string('street_more')->nullable();
$table->string('city');
$table->string('state')->nullable();
$table->string('country');
$table->string('postal_code');
$table->integer('addressable_id');
$table->string('addressable_type');
});
해결책
Yes, morphOne 방법과 생각이어야 하나 이 경우에 사용: https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model.php#L811 .
또한 사용할 수 있는
$table->morphs('addressable');
대
$table->integer('addressable_id');
$table->string('addressable_type');
나는 사용 morphOne 사용자 지정 패키지에 대한 L4 및 작동합니다.
다른 팁
이 하나를 시도,이보다 더 morphOne 는 지원되지 않습니다.
public function address()
{
return $this->hasOne('App\Address', 'addressable_id','id')
->where('addressable_type', 'App\Model');
}
제휴하지 않습니다 StackOverflow