Create a new pivot model instance.

@param  \Nova\Database\ORM\Model  $parent
@param  array   $attributes
@param  string  $table
@param  bool    $exists
@param  string|null  $using
@return \Nova\Database\ORM\Relations\Pivot
760250
public function newPivot(Model $parent, array $attributes, $table, $exists, $using = null)
    {
        $className = $using ?: Pivot::class;

        return new $className($parent, $attributes, $table, $exists);
    }
115352
public function newRelatedInstanceFor(Model $parent)
    {
        $newInstance = $this->related->newInstance();

        if (is_array($this->localKey)) { //Check for multi-columns relationship
            $foreignKey = $this->getForeignKeyName();

            foreach ($this->localKey as $index => $key) {
                $newInstance->setAttribute($foreignKey[$index], $parent->{$key});
            }
        } else {
            return $newInstance->setAttribute($this->getForeignKeyName(), $parent->{$this->localKey});
        }
    }
515517
public function sqlSelect(
		array $attributes,
		$from,
		$where = null,
		array $params = [],
		$group_by = null,
		$order_by = null,
		$limit = null,
		$start = 0
	) {
		$sql = $this->queryBuilder->buildSelect(
			$attributes,
			$from,
			$where,
			$group_by,
			$order_by,
			$limit,
			$start
		);
		$query = $this->database->prepare($sql);
		$query->execute($params);
		$this->addQuery($query->queryString, $params);
		return $query;
	}
72875
public function createModel(iterable $attributes): self
    {
        $this->insert($this->quoteSingleIdentifier($this->getTableName()));

        $valuesAsParams = [];
        foreach ($this->bindAttributes($this->getModelClass(), $attributes) as $quotedColumn => $parameterName) {
            $valuesAsParams[$quotedColumn] = $parameterName;
        }
        $this->values($valuesAsParams);

        return $this;
    }
1007076
public static function newFromBuilderRecursive(Model $model, array $attributes = [], Relation $parentRelation = null)
    {
        $instance = $model->newInstance([], $exists = true);

        $instance->setRawAttributes((array)$attributes, $sync = true);

        // Load relations recursive
        static::loadRelationsAttributesRecursive($instance);
        // Load pivot
        static::loadPivotAttribute($instance, $parentRelation);

        return $instance;
    }
623424
public function newPivot(array $attributes = [], $exists = false)
    {

        $using = $this->using;

        $pivot = $using ? $using::fromRawAttributes($this->parent, $attributes, $this->table, $exists)
                        : new MorphPivot($attributes, $this->parent, $this->table, $exists);

        $pivot->setPivotKeys($this->foreignKey, $this->relatedKey)
              ->setMorphType($this->morphType)
              ->setMorphClass($this->morphClass);

        return $pivot;
    }
1218580
public function create(array $attributes, array $joining = array(), $touch = true)
	{
		$instance = $this->related->newInstance($attributes);

		// Once we save the related model, we need to attach it to the base model via
		// through intermediate table so we'll use the existing "attach" method to
		// accomplish this which will insert the record and any more attributes.
		$instance->save(array('touch' => false));

		$this->attach($instance->getKey(), $joining, $touch);

		return $instance;
	}
1033091
protected function createChildContext(array $parentContext, $attribute/*, string $format = null */)
    {
        if (\func_num_args() >= 3) {
            $format = \func_get_arg(2);
        } else {
            // will be deprecated in version 4
            $format = null;
        }

        $context = parent::createChildContext($parentContext, $attribute, $format);
        // format is already included in the cache_key of the parent.
        $context['cache_key'] = $this->getCacheKey($format, $context);

        return $context;
    }
1181910
public function newPivot(array $attributes = [], $exists = false)
	{
		$pivot = $this->related->newPivot($this->parent, $attributes, $this->table, $exists);

		return $pivot->setPivotKeys($this->foreignKey, $this->otherKey);
	}
967146
public static function loadPivotAttribute(Model $model, Relation $parentRelation = null)
    {
        $attributes = $model->getAttributes();

        foreach ($attributes as $key => $value) {
            if ($key === 'pivot') {
                unset($model[$key]);
                $pivot = $parentRelation->newExistingPivot($value);
                $model->setRelation($key, $pivot);
            }
        }
    }
1250834
protected function _merge($data, $exists)
    {
        if (!$data) {
            $this->_parent->get($this->_through)->clear();
            return;
        }

        $pivot = $this->_parent->{$this->_through};

        $relThrough = $this->_parent->schema()->relation($this->_through);
        $through = $relThrough->to();
        $schema = $through::definition();
        $rel = $schema->relation($this->_using);
        $fromKey = $rel->keys('from');
        $toKey = $rel->keys('to');

        $i = 0;

        while ($i < $pivot->count()) {
            $found = false;
            $entity = $pivot->get($i);
            $id1 = $entity->get($fromKey);
            if ($id1 === null) {
                $pivot->splice($i, 1);
                continue;
            }
            foreach ($data as $key => $item) {
                $isDocument = $item instanceof Document;
                $id2 = $isDocument ? $item->get($toKey) : (isset($item[$toKey]) ?$item[$toKey] : null);

                if ((string) $id1 === (string) $id2) {
                    if ($isDocument) {
                        $entity->set($this->_using, $item);
                    } else {
                        $entity->get($this->_using)->amend($item);
                    }
                    unset($data[$key]);
                    $i++;
                    $found = true;
                    break;
                }
            }
            if (!$found) {
                $pivot->splice($i, 1);
            }
        }

        foreach ($data as $entity) {
            $this[] = $entity;
        }
    }
1027916
public function createTable(array $options = [], array $attributes = []) {
        $schema = $this->getSchema();
        $schema->addOptions($options);

        // Create the table
        $status = (bool) $this->query(Query::CREATE_TABLE)
            ->attribute($attributes)
            ->schema($schema)
            ->save();

        // Create the indexes
        if ($status) {
            foreach ($schema->getIndexes() as $index => $columns) {
                $this->query(Query::CREATE_INDEX)
                    ->from($schema->getTable(), $index)
                    ->save($columns);
            }
        }

        return $status;
    }
921199
protected function createMissingAbilities($existing, array $abilities, $attributes = [])
    {
        $missing = array_diff($abilities, $existing->pluck('name')->all());

        return array_map(function ($ability) use ($attributes) {
            return Models::ability()->create($attributes + ['name' => $ability]);
        }, $missing);
    }
1345975
public function makeChild(array $attributes=[], $parent)
    {
        $child = $this->model()->makeChild($attributes, $parent);
        $this->callAfterListeners('make', [$child]);
        return $child;
    }
102425
public function fill(array $attributes, $exists = false)
    {
        $this->fillExists = $exists;

        foreach ($attributes as $key => $value) {
            $this->setAttribute($key, $value);
        }

        $this->fillExists = false;

        return $this;
    }
1065924
public function outerJoin($table, $where, $using = true)
    {
        //argument test
        Argument::i()
            //Argument 1 must be a string
            ->test(1, 'string')
            //Argument 2 must be a string
            ->test(2, 'string')
            //Argument 3 must be a boolean
            ->test(3, 'bool');
        
        return $this->join('OUTER', $table, $where, $using);
    }
721445
public function newInstance($attributes = [], $exists = false)
    {
        return \tap(parent::newInstance($attributes, $exists), function ($model) {
            $model->setTenantor($this->tenantor ?? null);
        });
    }