내 코드가 정상적으로 작동한다고하지만 그 이유를 이해하지 못한다 ... 다른 "모듈"에 대한 사용자 정의 필드를 추가하고 싶습니다.내 다형성 관계에 대한 설명
예 : 카메라, 서버 및 custom_fields : 3 개의 테이블이 있습니다.
카메라 & 서버 모델 : 내가 컨트롤러에이 라인을 통해 몇 가지 요소를 추가 할 수 있습니다
Schema::create('description_fields', function (Blueprint $table) {
$table->increments('id');
$table->integer('item_id')->unsigned();
$table->string('item_type');
$table->string('name');
$table->string('value');
$table->timestamps();
});
:
$camera->custom_fields()->attach($request->custom_field);
내 질문
public function custom_fields()
{
return $this->morphToMany('App\Models\Camera', 'item', 'description_fields', '', 'name')
->withPivot('name', 'value')
->withTimestamps();
}
이 관계에 대한 표 모델에 관한 것입니다. 이유는 무엇입니까?
morphToMany ('앱 \ 모델 \ 카메라', '아이템', 'description_fields', '', '이름') 나는이 개 마지막 매개 변수를 지정해야하는 이유
이이해가 안 : ' ','name '('name '을'value '로 변경하면 작동하지만,' '을 삭제하면'name '이 작동하지 않습니다.
나는 params에 대한 문서를 읽었지만 여전히 이해할 수는 없다. (나는 전문 개발자는 아니지만 혼자서 배울 수있다.) 누군가 5 분을 설명해 주면 나에게 감사 할 것입니다.
미리 감사드립니다. 이름 '과 $relatedKey
'
/**
* Define a polymorphic many-to-many relationship.
*
* @param string $related
* @param string $name
* @param string $table
* @param string $foreignKey
* @param string $relatedKey
* @param bool $inverse
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function morphToMany($related, $name, $table = null, $foreignKey = null, $relatedKey = null, $inverse = false)
{
$caller = $this->guessBelongsToManyRelation();
// First, we will need to determine the foreign key and "other key" for the
// relationship. Once we have determined the keys we will make the query
// instances, as well as the relationship instances we need for these.
$instance = $this->newRelatedInstance($related);
$foreignKey = $foreignKey ?: $name.'_id';
$relatedKey = $relatedKey ?: $instance->getForeignKey();
// Now we're ready to create a new query builder for this related model and
// the relationship instances for this relation. This relations will set
// appropriate query constraints then entirely manages the hydrations.
$table = $table ?: Str::plural($name);
return new MorphToMany(
$instance->newQuery(), $this, $name, $table,
$foreignKey, $relatedKey, $caller, $inverse
);
}
당신은 $foreignKey
을 설정하는에 '
빠른 답장을 보내 주셔서 감사합니다. 그것은 일부 데이터를 추가 할 때만 작동하지만 카메라에서 사용자 정의 필드를 가져 오려는 경우 작동하지 않습니다. 나는 내 관계가 정확하지 않다고 생각하지만 감사합니다. 나는 그것을 얻으려고 노력할 것입니다! – alexcool68
나는이 관계로 관계를 다시 작성했다 : returnTo this-> morphToMany ('App \ Models \ Server', 'item', 'description_fields', '', 'item_id') -> withPivot ('name' , 'value') -> withTimestamps(); ' 이제는 정상적으로 작동하지만 블레이드에서는'{{$ custom_field-> pivot-> name}}'을 사용해야합니다. 내가 알았어! – alexcool68