has() public method

Add a relationship count / exists condition to the query.
public has ( string $relation, string $operator = '>=', integer $count = 1, string $boolean = 'and', Closure $callback = null ) : Builder | static
$relation string
$operator string
$count integer
$boolean string
$callback Closure
return Builder | static
Beispiel #1
0
 /**
  * Scope of sellers.
  *
  * @param \Illuminate\Database\Eloquent\Builder $query
  *
  * @return \Illuminate\Database\Eloquent\Builder
  */
 public function scopeSellers($query)
 {
     return $query->has('items');
 }
Beispiel #2
0
 /**
  * @param Builder $builder
  * @return Builder|static
  */
 public function scopeTranslated(Builder $builder)
 {
     return $builder->has('translations');
 }
 /**
  * Scope for a Model that doesn't have any tags.
  *
  * @param \Illuminate\Database\Eloquent\Builder $query
  *
  * @return \Illuminate\Database\Eloquent\Builder
  */
 public function scopeWithoutTags(Builder $query)
 {
     return $query->has('tags', '=', 0);
 }
 public function scopeWithAnyTags(Builder $query, $tags = [])
 {
     $tags = Util::buildTagArray($tags);
     if (empty($tags)) {
         return $query->has('tags');
     }
     $slug = array_map([S::class, 'slugify'], $tags);
     return $query->whereHas('tags', function ($q) use($slug) {
         $q->whereIn('slug', $slug);
     });
 }
Beispiel #5
0
 /**
  * Gets the roles with no users.
  *
  * @param \Illuminate\Database\Eloquent\Builder $query
  * @return \Illuminate\Database\Eloquent\Builder
  */
 public function scopeInactive($query)
 {
     return $query->has('users', '<', 1);
 }
Beispiel #6
0
 public function scopeActive(Builder $query)
 {
     $query->has('attributeOptions');
 }
Beispiel #7
0
 /**
  * Scope of categories with no children.
  *
  * @param \Illuminate\Database\Eloquent\Builder $query
  *
  * @return \Illuminate\Database\Eloquent\Builder
  */
 public function scopeLeaves($query)
 {
     return $query->has('children', 0);
 }
Beispiel #8
0
 public function apply(Builder $builder, Model $model)
 {
     $builder->has('article');
     $this->addWithDrafts($builder);
 }
Beispiel #9
0
 /**
  * @param Builder $model
  * @return mixed
  */
 public function applyToQuery($model)
 {
     return $model->has($this->relation, $this->operator, $this->count, $this->boolean, $this->callback);
 }
 /**
  * Query has relations.
  *
  * @param  \Illuminate\Database\Eloquent\Builder $query
  * @param  array                                 $hasRelations
  * @return \Illuminate\Database\Eloquent\Builder
  */
 protected function queryHasRelations($query, $hasRelations = [])
 {
     $hasRelations = $hasRelations ?: $this->hasRelations;
     foreach ($hasRelations as $relation => $operatorCount) {
         list($operator, $count) = $operatorCount;
         $query->has($relation, $operator, $count);
     }
     return $query;
 }
 /**
  * @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'));
     });
     // Hide discussions with no tags if the user doesn't have that global
     // permission.
     if (!$actor->hasPermission('viewDiscussions')) {
         $query->has('tags');
     }
 }
Beispiel #12
0
 /**
  * Scope a query to only include online players.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $query
  * @return \Illuminate\Database\Eloquent\Builder
  */
 public function scopeOnline($query)
 {
     return $query->has('playerOnline');
 }
Beispiel #13
0
 /**
  * @param Builder $query
  * @param UserInterface $user
  * @return Builder
  */
 public function scopeForUser(Builder $query, UserInterface $user = null)
 {
     if ($user === null && $this->user !== null) {
         $user = $this->user;
     }
     if ($user === null) {
         return $query->has('activities');
     }
     return $query->whereHas('activities', function ($q) use($user) {
         $q->where('user_id', '=', $user->getAuthIdentifier());
     });
 }
Beispiel #14
0
 /**
  * Query wether or not the relationship is empty of now.
  *
  * @param        $relation
  * @param string $operator
  * @param int    $count
  *
  * @return $this
  */
 public function has($relation, $operator = '>=', $count = 1)
 {
     $this->builder = $this->builder->has($relation, $operator, $count);
     return $this;
 }
 /**
  * @param \Illuminate\Database\Eloquent\Builder $query
  *
  * @return \Illuminate\Database\Eloquent\Builder|static
  */
 public function scopeTranslated(Builder $query)
 {
     return $query->has('translations');
 }
Beispiel #16
0
 /**
  * @param Builder $query
  *
  * @return Builder
  */
 public function scopeInStock(Builder $query) : Builder
 {
     return $query->has('availableStock');
 }
Beispiel #17
0
 /**
  * Add a relationship count condition to the query.
  *
  * @param string $relation
  * @param string $operator
  * @param int $count
  * @param string $boolean
  * @param \Closure|null $callback
  * @return \Illuminate\Database\Eloquent\Builder|static 
  * @static 
  */
 public static function has($relation, $operator = '>=', $count = 1, $boolean = 'and', $callback = null)
 {
     return \Illuminate\Database\Eloquent\Builder::has($relation, $operator, $count, $boolean, $callback);
 }
 /**
  * get has a minimum one published child element of category
  *
  * @param \Illuminate\Database\Eloquent\Builder $query
  * @param string $relation
  * @return \Illuminate\Database\Eloquent\Builder
  */
 public function scopeHasPublishedElement($query, $relation)
 {
     return $query->has($relation, '>=', 1, 'and', function ($query) {
         return $query->published();
     });
 }