범주에 다형성 관계가 있으며 클라이언트 또는 템플릿에 속할 수 있습니다.Laravel 저장시 다형성 관계 덮어 쓰기
템플릿을 클라이언트에 할당하려고합니다. 하지만 내 템플릿 카테고리를 덮어 쓰고 있습니다.
분류 모델
class Category extends Model
{
public function categorisable()
{
return $this->morphTo();
}
}
클라이언트 모델
class Client extends Model
{
public function categories()
{
return $this->morphMany(Category::class, 'categorisable');
}
}
템플릿 모델
class Template extends Model
{
public function categories()
{
return $this->morphMany(Category::class, 'categorisable');
}
}
이것은 내가 클라이언트
$template = Template::find($request->get('template_id'));
$client = Client::find($request->get('client_id'));
// For this template, get all categories
foreach($template->categories as $category) {
$client->categories()->save($category);
}
에 템플릿에서 카테고리를 복사하기 위해 노력하고있어하지만 그것은 내 카테고리 테이블에서이 개 범주를 덮어 쓰는 것 코드입니다. 따라서 클라이언트에 저장하고 템플릿에서 제거하십시오.
도움이 될 것입니다. 감사합니다.