src/Collection/Mutable.php
public function insert($element, $position, Closure $callback = null): self
    {
        $element = $this->createElement($element);

        if ($callback) {
            $callback($element);
        }

        if (is_string($position)) {
            $this->push($element);

            return $this->move($element->id(), $position);
        }

        if ($position >= $this->count()) {
            return $this->push($element);
        }

        if (0 === $position) {
            return $this->prepend($element);
        }

        $items = [];
        foreach ($this->all() as $index => $value) {
            if ($index === $position) {
                array_push($items, $element);
            }

            array_push($items, $value);
        }
        $this->items = $items;

        return $this;
    }
public function insert($element, $position, Closure $callback = null): self
    {
        $element = $this->createElement($element);

        if ($callback) {
            $callback($element);
        }

        if (is_string($position)) {
            $this->push($element);

            return $this->move($element->id(), $position);
        }

        if ($position >= $this->count()) {
            return $this->push($element);
        }

        if (0 === $position) {
            return $this->prepend($element);
        }

        $items = [];
        foreach ($this->all() as $index => $value) {
            if ($index === $position) {
                array_push($items, $element);
            }

            array_push($items, $value);
        }
        $this->items = $items;

        return $this;
    }
public function insert ( $ element , $ position , Closure $ callback = null ) : self { $ element = $ this -> createElement ( $ element ) ; if ( $ callback ) { $ callback ( $ element ) ; } if ( is_string ( $ position ) ) { $ this -> push ( $ element ) ; return $ this -> move ( $ element -> id ( ) , $ position ) ; } if ( $ position >= $ this -> count ( ) ) { return $ this -> push ( $ element ) ; } if ( 0 === $ position ) { return $ this -> prepend ( $ element ) ; } $ items = [ ] ; foreach ( $ this -> all ( ) as $ index => $ value ) { if ( $ index === $ position ) { array_push ( $ items , $ element ) ; } array_push ( $ items , $ value ) ; } $ this -> items = $ items ; return $ this ; }
Insert an element into collection at specified position.

@param $element
@param $position
@param null|Closure $callback

@return $this
Insert an element into collection at specified position .