code: public function storeChildAndPivot($idParent, $relation, $data = [])
      {
          $parent = $this->find($idParent);
          $childEntity = $parent->$relation()->getRelated();
          
          $child = $childEntity->create($data);
          
          $data = $this->onlyFillablePivot($parent->$relation(), $data);
          
          $parent->$relation()->attach($child->id, $data);
          
          return $child;
      }
docs: Attach relation
  
  @return \Illuminate\Database\Eloquent\Model;