Adds random score function. Seed is optional.

@param mixed            $seed
@param BuilderInterface $query

@return $this
961575
public function addRandomFunction($seed = null, BuilderInterface $query = null)
    {
        $function = [
            'random_score' => $seed ? [ 'seed' => $seed ] : new \stdClass(),
        ];

        $this->applyFilter($function, $query);

        $this->functions[] = $function;

        return $this;
    }
371846
public function add($seed, $region = null)
    {
        if ($this->_rendered) {
            throw new Exception('You cannot add anything into the view after it was rendered');
        }
        if (!$this->app) {
            $this->_add_later[] = [$seed, $region];

            return $seed;
        }

        if (is_array($region)) {
            $args = $region;
            if (isset($args['region'])) {
                $region = ['region'=>$args['region']];
                unset($args['region']);
            }
        } elseif ($region) {
            $args = null;
            $region = ['region'=>$region];
        } else {
            $args = null;
            $region = null;
        }

        // Create object first
        $object = $this->factory($this->mergeSeeds($seed, ['View']), $region);

        // Will call init() of the object
        $object = $this->_add($object, $args);

        return $object;
    }
176120
protected function performInsert(Builder $query)
    {
        $encryptedFields = static::getEncryptedFields();
        if (count($encryptedFields) && !$this->getEncryptKey()) {
            throw new \RuntimeException("No encryption key specified");
        }
        $originalAttributes = $this->attributes;
        foreach ($encryptedFields as $encryptedField) {
            if (isset($this->attributes[$encryptedField])) {
                $this->attributes[$encryptedField] = DB::raw(static::getEncryptionService()->getEncryptExpression($this->attributes[$encryptedField], static::getEncryptKey()));
            }
        }
        $inserted = parent::performInsert($query);

        // reset the attributes to the decrypted values
        foreach ($encryptedFields as $encryptedField) {
            if (isset($this->attributes[$encryptedField])) {
                $this->attributes[$encryptedField] = $originalAttributes[$encryptedField];
            }
        }
        return $inserted;
    }
1222979
public function alterQueryBuilderExceptWithParentRoot($query, $parent_id)
    {
        if (is_null($parent_id)) {
            return $query;
        }
        $parent = $this->find($parent_id);
        $query->andWhere(
            $query->getRootAliases()[0] . '.lft > ' . $this->addNamedParameter('rgt', $parent->getRgt(), $query)
            . ' OR ' . $query->getRootAliases()[0] . '.lft < ' . $this->addNamedParameter('lft', $parent->getLft(), $query)
        );
        return $query;
    }
908323
private function setLastInsertId(QueryBuilder $query)
    {
        $seq = null;
        if ($query->getConnection()->getDatabasePlatform()->getName() === 'postgresql') {
            $sequences = $query->getConnection()->getSchemaManager()->listSequences();
            $desiredSeq = $query->getQueryPart('from')['table'] . '_id_seq';
            foreach ($sequences as $sequence) {
                if ($desiredSeq === $sequence->getName()) {
                    $seq = $desiredSeq;
                    break;
                }
            }
            if ($seq === null) {
                return;
            }
        }
        $this->lastInsertId = $query->getConnection()->lastInsertId($seq);
    }
881009
protected function applySortingToQueryBuilder($query, $source, $direction = 'asc', $strategy = null)
    {
        $source = $source ?: $query->getModel()->getKeyName();

        // Prepare the sorting strategy
        if (null === $strategy) {
            $strategy = config('cms-models.meta-references.sort-strategy');
        }

        if ( ! $strategy) return $this;


        $strategyInstance = new $strategy;

        if ( ! ($strategyInstance instanceof SortStrategyInterface)) {
            throw new UnexpectedValueException("{$strategy} is not a sort strategy");
        }

        // Explode combined source keys and apply the strategy for each part
        foreach (explode(',', $source) as $sourcePart) {

            $query = $strategyInstance->apply($query, $sourcePart, $direction);
        }

        return $this;
    }
1270922
public static function shuffle($array, $seed = null)
    {
        if (\is_null($seed)) {
            shuffle($array);
        } else {
            srand($seed);

            usort($array, function () {
                return rand(-1, 1);
            });
        }

        return $array;
    }
89149
protected function applyWhere(Builder $query, array $where)
    {
        foreach ($where as $key => $value) {
            if (is_null($value)) {
                $query = $query->whereNull($key);
            } else {
                if (is_array($value)) {
                    $query = $query->where($key, $value[0], $value[1]);
                } else {
                    $query = $query->where($key, '=', $value);
                }
            }
        }

        return $query;
    }
962616
public function createSearch($query = '', $options = null, BuilderInterface $builder = null)
    {
        $search = new Search($this->getClient(), $builder);
        $search->addIndex($this);
        $search->setOptionsAndQuery($options, $query);

        return $search;
    }
30806
public function setSeed($seed)
    {
        if (is_int($seed) !== true) {
            throw new Exception(
                sprintf('The type of the seed value "%s" need to be int. You passed a(n) "%s".', $seed, gettype($seed))
            );
        }

        // We need to call different methods depending on chosen source
        switch ($this->getMode()) {

            case self::MODE_PHP_MERSENNE_TWISTER:
                mt_srand($seed);
                break;

            case self::MODE_PHP_DEFAULT:
                srand($seed);
                break;

            case self::MODE_OPEN_SSL:
            default:
                // Intentionally left blank
                break;
        }

        $this->seed = $seed;
    }
716708
protected function setKeysForSaveQuery(Builder $query)
    {
        $keys = $this->getKeyName();

        if (!is_array($keys)) {
            return parent::setKeysForSaveQuery($query);
        }

        foreach ($keys as $key) {
            $query->where($key, '=', $this->getKeyForSaveQuery($key));
        }

        return $query;
    }
1280087
public function seed($seed = null)
    {
        $this->seed = $seed;
        srand($this->seed);
        
        return $this;
    }
436334
public function shuffle($seed = null)
    {
        $items = $this->items;

        if (is_null($seed)) {
            shuffle($items);
        } else {
            srand($seed);

            usort($items, function () {
                return rand(-1, 1);
            });
        }

        return new static($items);
    }
774601
protected function setKeysForSaveQuery(Builder $query)
	{
		$query->where($this->morphType, $this->getAttribute($this->morphType));

		return parent::setKeysForSaveQuery($query);
	}
90077
protected function performJoin(Builder $query = null)
    {
        $query = $query ?: $this->query;

        $farKey = $this->getQualifiedFarKeyName();

        $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $farKey);

        if ($this->throughParentSoftDeletes()) {
            $query->whereNull($this->throughParent->getQualifiedDeletedAtColumn());
        }
    }
1344002
public function setQuery(\Illuminate\Database\Eloquent\Builder &$query): PaginationInterface
    {
        $this->_query = $query;

        return $this;
    }
825572
protected function setKeysForSaveQuery(Builder $query)
    {
        if (isset($this->attributes[$this->getKeyName()])) {
            return parent::setKeysForSaveQuery($query);
        }

        $query->where($this->foreignKey, $this->getOriginal(
            $this->foreignKey, $this->getAttribute($this->foreignKey)
        ));

        return $query->where($this->relatedKey, $this->getOriginal(
            $this->relatedKey, $this->getAttribute($this->relatedKey)
        ));
    }