src/Repositories/Repository.php
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;
    }
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;
    }
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 ; }
Attach relation

@return \Illuminate\Database\Eloquent\Model;
Attach relation