本文整理汇总了PHP中Illuminate\Database\Eloquent\Builder类的典型用法代码示例。如果您正苦于以下问题:PHP Builder类的具体用法?PHP Builder怎么用?PHP Builder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Builder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: scopeWithTag
public static function scopeWithTag(Builder $query, $tags, $type = 'slug')
{
$tags = (new static())->prepareTags($tags);
return $query->whereHas('tags', function ($query) use($type, $tags) {
$query->whereIn($type, $tags);
});
}
示例2: process
/**
* @param Builder $src
* @param OperationInterface|SortOperation $operation
* @return mixed
*/
public function process($src, OperationInterface $operation)
{
$field = $operation->getField();
$order = $operation->getOrder();
$src->orderBy($field, $order);
return $src;
}
示例3: apply
/**
* Apply the scope to a given Eloquent query builder.
* @param Builder $builder
* @param Model $model
*/
public function apply(Builder $builder, Model $model)
{
$builder->join('uris', function ($join) use($model) {
$join->where('uris.owner_type', '=', get_class($model));
$join->on('uris.owner_id', '=', $model->getTable() . '.' . $model->getKeyName());
})->select([$model->getTable() . '.*', 'uris.uri']);
}
示例4: __construct
/**
* Create a new relation instance.
*
* @param Illuminate\Database\Eloquent\Builder
* @param Illuminate\Database\Eloquent\Model
* @return void
*/
public function __construct(Builder $query, Model $parent)
{
$this->query = $query;
$this->parent = $parent;
$this->related = $query->getModel();
$this->addConstraints();
}
示例5: scopeVisibleTo
/**
* Scope a query to only include records that are visible to a user.
*
* @param Builder $query
* @param User $actor
* @return Builder
*/
protected function scopeVisibleTo(Builder $query, User $actor = null)
{
if ($actor !== null) {
$query->whereVisibleTo($actor);
}
return $query;
}
示例6: handle
/**
* Handle the filter.
*
* @param Builder $query
* @param SearchFilterInterface $filter
*/
public function handle(Builder $query, TableBuilder $builder, SearchFilterInterface $filter)
{
$stream = $filter->getStream();
$model = $builder->getTableModel();
/**
* If the model is translatable then
* join it's translations so they
* are filterable too.
*
* @var EloquentQueryBuilder $query
*/
if ($model->getTranslationModelName() && !$query->hasJoin($model->getTranslationTableName())) {
$query->leftJoin($model->getTranslationTableName(), $model->getTableName() . '.id', '=', $model->getTranslationTableName() . '.' . $model->getRelationKey());
}
$query->where(function (Builder $query) use($filter, $stream) {
foreach ($filter->getColumns() as $column) {
$query->orWhere($column, 'LIKE', "%{$filter->getValue()}%");
}
foreach ($filter->getFields() as $field) {
$filter->setField($field);
$fieldType = $stream->getFieldType($field);
$fieldTypeQuery = $fieldType->getQuery();
$fieldTypeQuery->setConstraint('or');
$this->container->call([$fieldTypeQuery, 'filter'], compact('query', 'filter', 'builder'));
}
});
}
示例7: find
/**
* @param User $actor
* @param Builder $query
*/
public function find(User $actor, Builder $query)
{
// Hide discussions which have tags that the user is not allowed to see.
$query->whereNotExists(function ($query) use($actor) {
return $query->select(new Expression(1))->from('discussions_tags')->whereIn('tag_id', Tag::getIdsWhereCannot($actor, 'viewDiscussions'))->where('discussions.id', new Expression('discussion_id'));
});
}
示例8: sort
/**
* @param Builder $query
* @param SortInterface $sort
*/
public static function sort(Builder $query, SortInterface $sort)
{
/** @var Order $order */
foreach ($sort->orders() as $propertyName => $order) {
$query->getQuery()->orderBy($propertyName, $order->isAscending() ? 'ASC' : 'DESC');
}
}
示例9: processWhere
/**
* @param Builder $query
* @param $wheres
*/
private function processWhere(Builder $query, $wheres)
{
$self = $this;
foreach ($wheres as $where) {
/** @var WhereParameter $where */
$query->where(function (Builder $query) use($self, $where) {
if ($comparison = $where->getComparison()) {
$query->where($comparison->getSubject(), $comparison->getOperator(), $comparison->getValue());
}
foreach ($where->getChildren() as $child) {
if ($child instanceof AndConjunction) {
$query->where(function (Builder $query) use($self, $child) {
$this->processWhere($query, [$child->getSubject()]);
});
} elseif ($child instanceof OrConjunction) {
$query->orWhere(function (Builder $query) use($self, $child) {
$this->processWhere($query, [$child->getSubject()]);
});
} else {
throw new \InvalidArgumentException("Got an unknown conjunction");
}
}
});
}
}
示例10: build
public function build(Builder $query)
{
$page = $this->page;
return $query->join('pages_tags', 'tags.id', '=', 'pages_tags.tag_id')->join('pages', 'pages_tags.page_id', '=', 'pages.id')->where(function ($query) use($page) {
$query->where('pages.id', '=', $page->getId())->orWhere('pages.parent_id', '=', $page->getId());
})->groupBy('tags.id')->orderBy('tags.name', 'asc');
}
示例11: addUnordered
/**
* Extend Builder with custom method
*
* @param \Illuminate\Database\Eloquent\Builder $builder
*/
protected function addUnordered(Builder $builder)
{
$builder->macro('unordered', function (Builder $builder) {
$this->remove($builder, $builder->getModel());
return $builder;
});
}
示例12: withRelatedCriterion
public function withRelatedCriterion(QueryBuilder $query)
{
if ($this->related === null) {
throw new \RuntimeException('No relationships defined in the repository. Eloquent does ' . 'not support reading relationships from the Model. Related ' . 'models must be defined in the $related property of the ' . 'repository.');
}
return $query->with($this->related);
}
示例13: setKeysForSaveQuery
/**
* Set the keys for a save update query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function setKeysForSaveQuery(Builder $query)
{
foreach ($this->getKeyName() as $key) {
$query->where($key, '=', $this->getAttribute($key));
}
return $query;
}
示例14: scopeSearch
/**
* @param Builder $query
*
* @return Builder
*/
public function scopeSearch($query)
{
if (Input::has('q')) {
$q = Input::get('q');
$model = $this;
$query->where(function ($query2) use($q, $model) {
foreach ($model->getColumns() as $searchColumn) {
$table = $model->getTable();
$searchColumnExplode = explode('.', $searchColumn);
if (count($searchColumnExplode) > 1) {
$table = $searchColumnExplode[0];
$searchColumn = $searchColumnExplode[1];
}
switch (Schema::getColumnType($table, $searchColumn)) {
case 'integer':
case 'bigint':
case 'smallint':
case 'float':
if (is_numeric($q)) {
$query2->orWhere($table . '.' . $searchColumn, '=', (int) $q);
}
break;
case 'string':
case 'text':
$query2->orWhere($table . '.' . $searchColumn, 'ilike', "%{$q}%");
break;
}
}
});
}
return $query;
}
示例15: scopeFindSimilarSlugs
/**
* Query scope for finding "similar" slugs, used to determine uniqueness.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $attribute
* @param array $config
* @param string $slug
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeFindSimilarSlugs(Builder $query, Model $model, $attribute, $config, $slug)
{
$separator = $config['separator'];
return $query->where(function (Builder $q) use($attribute, $slug, $separator) {
$q->where($attribute, '=', $slug)->orWhere($attribute, 'LIKE', $slug . $separator . '%');
});
}