Scope a query to only include records created by a given user.

@param \Illuminate\Database\Eloquent\Builder $query
@param \Illuminate\Database\Eloquent\Model $user

@return \Illuminate\Database\Eloquent\Builder
358389
public function scopeOnlyCreatedBy(Builder $query, Model $user)
    {
        return $query->where(config('accountable.column_names.created_by'), $user->getKey());
    }
1084212
public function collectForUser($query, $users, $options = [])
  {
    $bigFeed = [];

    foreach ($users as $userId) {
      $userOptions = array_merge($options, ['user' => $userId]);
      $userFeed = $this->getFeed($query, $userOptions);
      $bigFeed = array_merge($bigFeed, $userFeed);
    }

    $bigFeed = $this->filterDuplicates($bigFeed);

    return $this->mapByHash($bigFeed);
  }
92085
public function scopeFullTextSearchFilter($query, $q, $user_id)
    {
        if(!empty($q)){

            // fall back to basic search if $q is a single word
            if(!strpos($q, ' ')){
                return $this->scopeSearchFilter($query, $q);
            }


            return $query->whereRaw('id IN (
            		SELECT id FROM dids 
            		WHERE searchable @@ plainto_tsquery(?) AND user_id = ?)',
                [$q, $user_id]
            );
        }
    }
115319
protected function search(Builder $query)
    {
        $q = request('q');

        if (!$q || strlen($q) < $this->minimumSearchInputLength)
            return $query;

        return $this->searchQuery($query, $q);
    }
903879
public function scopeWhereLikedBy(Builder $query, $userId = null)
    {
        return app(LikeableServiceContract::class)
            ->scopeWhereLikedBy($query, LikeType::LIKE, $userId);
    }
1307949
public function scopeFiltersApply(Builder $query, array $filters = []): Builder
    {
        foreach ($filters as $filter) {
            if (! is_object($filter)) {
                $filter = app()->make($filter);
            }

            $query = $filter->filter($query);
        }

        return $query;
    }
41936
public function scopeWithGroup(Builder $query, string $group = null): Builder
    {
        return $group ? $query->where('group', $group) : $query;
    }
1356465
public function scopeCurrentVersion(Builder $query)
    {
        $subquery = $this->getCurrentVersionQuery();

        return $query
            ->select('version.*')
            ->addSelect('version.id as version:id')
            ->addSelect('version.created_at as version:created_at')
            ->addSelect('version.created_by as version:created_by')
            ->addSelect('pages.*')
            ->join(DB::raw('('.$subquery->toSql().') as v2'), 'pages.id', '=', 'v2.page_id')
            ->mergeBindings($subquery)
            ->join('page_versions as version', function (JoinClause $join) {
                $join
                    ->on('pages.id', '=', 'version.page_id')
                    ->on('v2.id', '=', 'version.id');
            });
    }
1217500
public function createUser(User $user)
    {
        $query = $this->handler->createInsertQuery();
        $query
            ->insertInto($this->handler->quoteTable('ezuser'))
            ->set(
                $this->handler->quoteColumn('contentobject_id'),
                $query->bindValue($user->id, null, \PDO::PARAM_INT)
            )->set(
                $this->handler->quoteColumn('login'),
                $query->bindValue($user->login)
            )->set(
                $this->handler->quoteColumn('email'),
                $query->bindValue($user->email)
            )->set(
                $this->handler->quoteColumn('password_hash'),
                $query->bindValue($user->passwordHash)
            )->set(
                $this->handler->quoteColumn('password_hash_type'),
                $query->bindValue($user->hashAlgorithm, null, \PDO::PARAM_INT)
            );
        $query->prepare()->execute();

        $query = $this->handler->createInsertQuery();
        $query
            ->insertInto($this->handler->quoteTable('ezuser_setting'))
            ->set(
                $this->handler->quoteColumn('user_id'),
                $query->bindValue($user->id, null, \PDO::PARAM_INT)
            )->set(
                $this->handler->quoteColumn('is_enabled'),
                $query->bindValue($user->isEnabled, null, \PDO::PARAM_INT)
            )->set(
                $this->handler->quoteColumn('max_login'),
                $query->bindValue($user->maxLogin, null, \PDO::PARAM_INT)
            );
        $query->prepare()->execute();
    }
795052
public function applyToQuery(Builder $query)
    {
        $this->query = $query;

        foreach ($this->request->all() as $filter => $value) {
            $method = Str::camel($filter);

            if ($this->filterCanBeApplied($method, $value)) {
                call_user_func([$this, $method], $value);
            }
        }

        return $query;
    }
261128
public function getQbLastByUser(User $user)
    {
        $qb = $this->createQueryBuilder('d')
            ->where('d.createdBy = :user1 OR d.updatedBy = :user2')
            ->orderBy('d.updatedAt', 'desc')
            ->setParameters(
                [
                    'user1' => $user,
                    'user2' => $user,
                ]
            );

        return $qb;
    }
129009
public function scopeWhereRatedBy(Builder $query, $userId): Builder
    {
        $userId = $this->getRaterUserId($userId);

        return $query->whereHas('ratings', function (Builder $innerQuery) use ($userId) {
            $innerQuery->where('user_id', $userId);
        });
    }
604818
public function fromQuery(Query $query, ApiResource $resource)
    {
        /** @var \Phalcon\Mvc\Model\Manager $modelsManager */
        $modelsManager = $this->di->getShared('modelsManager');

        /** @var \Phalcon\Mvc\Model\Query\Builder $builder */
        $builder = $modelsManager->createBuilder()->from($resource->getModel());

        $this->applyQuery($builder, $query, $resource);

        return $builder;
    }
737348
public function scopeFiltersApplySelection(Builder $query, $selection)
    {
        if (! is_object($selection)) {
            $selection = app()->make($selection);
        }

        $filters = $selection->filters();

        return $this->scopeFiltersApply($query, $filters);
    }
890556
public function build(Builder $query)
    {
        if ($this->page === null) {
            return $query->whereNull('parent_id');
        }

        list($col, $direction) = $this->page->getChildOrderingPolicy();

        return $query
            ->where('parent_id', '=', $this->page->getId())
            ->orderBy($col, $direction);
    }
21809
public function scopeWhereSubject(Builder $query, $model)
    {
        $query->whereSubjectModel(get_class($model))
            ->where('subject_id', $model->id);
    }
393952
public function scopeProtectAdmins(Builder $query)
    {
        /** @var  self  $user */
        $user = auth()->user();

        return ($user && $user->is_admin)
            ? $query
            : $query->where('is_admin', false);
    }