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);
     });
 }
 /**
  * @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;
 }
Esempio n. 3
0
 /**
  * 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']);
 }
Esempio n. 4
0
 /**
  * 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();
 }
Esempio n. 5
0
 /**
  * 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;
 }
 /**
  * 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'));
         }
     });
 }
Esempio n. 7
0
 /**
  * @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'));
     });
 }
 /**
  * @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');
     }
 }
 /**
  * @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");
                 }
             }
         });
     }
 }
 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');
 }
Esempio n. 11
0
 /**
  * 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;
     });
 }
 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);
 }
Esempio n. 13
0
 /**
  * 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;
 }
Esempio n. 14
0
 /**
  * @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;
 }
Esempio n. 15
0
 /**
  * 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 . '%');
     });
 }
 protected function addWithDrafts(Builder $builder)
 {
     $builder->macro('withDrafts', function (Builder $builder) {
         $this->remove($builder, $builder->getModel());
         return $builder;
     });
 }
Esempio n. 17
0
 /**
  * 重写插入方法
  * param Builder $query
  * param $attributes
  */
 protected function insertAndSetId(Builder $query, $attributes)
 {
     //默认父ID
     $attributes[$this->treeField['parent_key']] = array_get($attributes, $this->treeField['parent_key'], 1);
     //初始化配置
     $this->treeInit(app('NestedSetsService'));
     //开启事务,处理边界
     DB::beginTransaction();
     //边界处理,返回修改值
     $attributes = $this->nestend->insert($attributes[$this->treeField['parent_key']], $attributes, 'bottom');
     //保存数据
     $id = $query->insertGetId($attributes, $keyName = $this->getKeyName());
     //结果提交
     if ($attributes !== false && $id) {
         DB::commit();
     } else {
         DB::rollback();
         return false;
     }
     //赋值
     $this->setAttribute($keyName, $id);
     $this->setAttribute($this->treeField['parent_key'], $attributes[$this->treeField['parent_key']]);
     $this->setAttribute($this->treeField['level_key'], $attributes[$this->treeField['level_key']]);
     $this->setAttribute($this->treeField['left_key'], $attributes[$this->treeField['left_key']]);
     $this->setAttribute($this->treeField['right_key'], $attributes[$this->treeField['right_key']]);
 }
 /**
  * Scope a query to only include records that are visible to a user.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $query
  * @param  \Flarum\Core\Models\User  $user
  * @return \Illuminate\Database\Eloquent\Builder
  */
 protected function scopeVisibleForUser(Builder $query, User $user = null)
 {
     if ($user !== null) {
         $query->whereCan($user, 'view');
     }
     return $query;
 }
Esempio n. 19
0
 public function build(Builder $query)
 {
     $text = $this->title;
     return $query->whereNested(function (QueryBuilder $query) use($text) {
         return $query->where('title', 'like', "%{$text}%")->orWhere('description', 'like', "%{$text}%");
     });
 }
Esempio n. 20
0
 /**
  * Constrain a query to an ability for a specific model.
  *
  * @param  \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder  $query
  * @param  \Illuminate\Database\Eloquent\Model  $model
  * @param  bool  $strict
  * @return void
  */
 protected function constrainByModel($query, Model $model, $strict = false)
 {
     $query->where(function ($query) use($model, $strict) {
         $query->where($this->table . '.entity_type', $model->getMorphClass());
         $query->where($this->abilitySubqueryConstraint($model, $strict));
     });
 }
 /**
  * Fired just before querying
  * for table entries.
  *
  * @param Builder $query
  */
 public function onQuerying(Builder $query)
 {
     $uploaded = $this->getUploaded();
     $query->whereIn('id', $uploaded ?: [0]);
     $query->orderBy('updated_at', 'ASC');
     $query->orderBy('created_at', 'ASC');
 }
Esempio n. 22
0
 /**
  * Apply the scope to a given Eloquent query builder.
  * @param Builder $builder
  * @param Model $model
  */
 public function apply(Builder $builder, Model $model)
 {
     $builder->join('product_translations', function ($join) {
         $join->on('products.id', '=', 'product_translations.product_id')->where('product_translations.locale', '=', app()->getLocale())->where('product_translations.published', '=', 1);
     });
     $builder->select(['products.*']);
 }
Esempio n. 23
0
 /**
  * Create a new morph to many relationship instance.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $query
  * @param  \Illuminate\Database\Eloquent\Model  $parent
  * @param  string  $name
  * @param  string  $table
  * @param  string  $foreignKey
  * @param  string  $otherKey
  * @param  string  $relationName
  * @param  bool  $inverse
  * @return void
  */
 public function __construct(Builder $query, Model $parent, $name, $table, $foreignKey, $otherKey, $relationName = null, $inverse = false)
 {
     $this->inverse = $inverse;
     $this->morphType = $name . '_type';
     $this->morphClass = $inverse ? $query->getModel()->getMorphClass() : $parent->getMorphClass();
     parent::__construct($query, $parent, $table, $foreignKey, $otherKey, $relationName);
 }
 /**
  * @param \Illuminate\Database\Eloquent\Builder $query
  * @param int                                   $id
  *
  * @return \Illuminate\Database\Eloquent\Builder
  */
 public function scopeEnabled($query, $id = null)
 {
     if ($id) {
         $query->where('id', '=', $id);
     }
     return $query->where('disabled', '=', false);
 }
Esempio n. 25
0
 /**
  * Get the siblings of the specified entry ID
  *
  * @param  \Illuminate\Database\Eloquent\Builder $query
  * @param  int|array                             $entryId
  * @return \Illuminate\Database\Eloquent\Builder
  */
 public function scopeSiblings(Builder $query, $entryId)
 {
     $entryId = is_array($entryId) ? $entryId : array($entryId);
     $connection = $query->getQuery()->getConnection();
     $tablePrefix = $connection->getTablePrefix();
     return $query->join('relationships', 'relationships.child_id', '=', 'channel_titles.entry_id')->join($connection->raw("`{$tablePrefix}relationships` AS `{$tablePrefix}relationships_2`"), 'relationships_2.parent_id', '=', 'relationships.parent_id')->addSelect('*')->addSelect('relationships_2.child_id AS sibling_id')->whereIn('relationships_2.child_id', $entryId)->orderBy('relationships.order', 'asc')->groupBy('relationships_2.child_id')->groupBy('relationships.field_id')->groupBy('channel_titles.entry_id');
 }
 /**
  * Fired just before querying
  * for table entries.
  *
  * @param Builder $query
  */
 public function onQuerying(Builder $query)
 {
     $uploaded = $this->getUploaded();
     if ($fieldType = $this->getFieldType()) {
         /*
          * If we have the entry available then
          * we can determine saved sort order.
          */
         $entry = $fieldType->getEntry();
         $table = $fieldType->getPivotTableName();
         if ($entry->getId() && !$uploaded) {
             $query->join($table, $table . '.file_id', '=', 'files_files.id');
             $query->where($table . '.entry_id', $entry->getId());
             $query->orderBy($table . '.sort_order', 'ASC');
         } else {
             $query->whereIn('id', $uploaded ?: [0]);
         }
     } else {
         /*
          * If all we have is ID then just use that.
          * The JS / UI will be handling the sort
          * order at this time.
          */
         $query->whereIn('id', $uploaded ?: [0]);
     }
 }
 public function processField2Filter(Builder $builder, Constraint $constraint)
 {
     if ($constraint->getOperator() == Constraint::OPERATOR_IN && is_array($constraint->getValue()) && $constraint->getValue()) {
         $builder->where('field2', $constraint->getValue()[0]);
         return true;
     }
 }
Esempio n. 28
0
 /**
  * Adds a sort scope.
  *
  * @param \Illuminate\Database\Eloquent\Builder $query
  * @param string                                $column
  * @param string                                $direction
  *
  * @return \Illuminate\Database\Eloquent\Builder
  */
 public function scopeSort(Builder $query, $column, $direction)
 {
     if (!in_array($column, $this->sortable)) {
         return $query;
     }
     return $query->orderBy($column, $direction);
 }
Esempio n. 29
0
 /**
  * Add the constraints for a relationship count query on the same table.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $query
  * @param  \Illuminate\Database\Eloquent\Builder  $parent
  * @return \Illuminate\Database\Eloquent\Builder
  */
 public function getRelationCountQueryForSelfRelation(Builder $query, Builder $parent)
 {
     $query->select(new Expression('count(*)'));
     $query->from($query->getModel()->getTable() . ' as ' . ($hash = $this->getRelationCountHash()));
     $key = $this->wrap($this->getQualifiedParentKeyName());
     return $query->where($hash . '.' . $this->getPlainForeignKey(), '=', new Expression($key));
 }
Esempio n. 30
0
 /**
  * Returns a new Issue table.
  *
  * @param Issue|Builder $issue
  * @param array         $with
  * @param Closure       $closure
  *
  * @return \Orchestra\Contracts\Html\Builder
  */
 public function table($issue, array $with = ['users', 'labels'], Closure $closure = null)
 {
     $label = request('label');
     // Filter issues with the specified request label.
     $issue->with($with)->label($label)->latest();
     return $this->table->of('issues', function (TableGrid $table) use($issue, $closure) {
         if ($closure instanceof Closure) {
             $table = call_user_func($closure, $table, $issue);
         } else {
             $table->with($issue)->paginate($this->perPage);
         }
         $table->sortable(['title', 'description', 'created_at']);
         $table->searchable(['title', 'description']);
         $table->column('status', function (Column $column) {
             $column->label = '';
             $column->value = function (Issue $issue) {
                 return $issue->present()->statusIcon();
             };
             $column->attributes(function () {
                 return ['width' => '30'];
             });
         });
         $table->column('title', function (Column $column) {
             return $this->tableTitle($column);
         });
     });
 }