Fetch records that are liked by a given user id.

@param \Illuminate\Database\Eloquent\Builder $query
@param string $type
@param int|null $userId
@return \Illuminate\Database\Eloquent\Builder

@throws \Cog\Likeable\Exceptions\LikerNotDefinedException
979855
public function scopeWhereLikedBy(Builder $query, $type, $userId)
    {
        $userId = $this->getLikerUserId($userId);

        return $query->whereHas('likesAndDislikes', function (Builder $innerQuery) use ($type, $userId) {
            $innerQuery->where('user_id', $userId);
            $innerQuery->where('type_id', $this->getLikeTypeId($type));
        });
    }
953861
public function fetchLikesCounters($likeableType, $likeType)
    {
        /** @var \Illuminate\Database\Eloquent\Builder $likesCount */
        $likesCount = app(LikeContract::class)
            ->select([
                DB::raw('COUNT(*) AS count'),
                'likeable_type',
                'likeable_id',
                'type_id',
            ])
            ->where('likeable_type', $likeableType);

        if (!is_null($likeType)) {
            $likesCount->where('type_id', $this->getLikeTypeId($likeType));
        }

        $likesCount->groupBy('likeable_id', 'type_id');

        return $likesCount->get()->toArray();
    }
683964
public function scopeForUser(Builder $query, $userId)
    {
        $participantsTable = Models::table('participants');
        $threadsTable = Models::table('threads');

        return $query->join($participantsTable, $this->getQualifiedKeyName(), '=', $participantsTable . '.thread_id')
            ->where($participantsTable . '.user_id', $userId)
            ->where($participantsTable . '.deleted_at', null)
            ->select($threadsTable . '.*');
    }
523167
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
	{
		if ($parent->getQuery()->from == $query->getQuery()->from) {
			return $this->getRelationQueryForSelfJoin($query, $parent, $columns);
		}

		$this->setJoin($query);

		return parent::getRelationQuery($query, $parent, $columns);
	}
1114532
public function findOneByWidgetType($type, $id, User $user, $executeQuery = true)
    {
        $queryBuilder = $this->createQueryBuilder('w')
            ->andWhere($this->getWigetInstanceString($type))
            ->andWhere('w.id = :id')
            ->andWhere('w.user = :user')
            ->setParameters([
                'id' => $id,
                'user' => $user,
            ])
        ;

        return $executeQuery ? $queryBuilder->getQuery()->getSingleResult() : $queryBuilder->getQuery();
    }
471051
public function scopePopular($query, $type = null)
    {
        $query->select('followable_id', 'followable_type', \DB::raw('COUNT(*) AS count'))
                        ->groupBy('followable_id', 'followable_type')
                        ->orderByDesc('count');

        if ($type) {
            $query->where('followable_type', $this->normalizeFollowableType($type));
        }

        return $query;
    }
97618
public function query($query, $type = '')
    {
        try {
            if ($type === 'SELECT') {
                return $this->conn->query($query);
            }

            return $this->conn->exec($query);
        } catch (\PDOException $e) {
            $this->error = $e->getMessage();

            return null;
        }
    }
663769
protected function scopeVisibleTo(Builder $query, User $user = null)
    {
        if ($user !== null) {
            $query->whereVisibleTo($user);
        }

        return $query;
    }
1283500
public function scopeOrderByLikesCount(Builder $query, $direction = 'desc')
    {
        return app(LikeableServiceContract::class)
            ->scopeOrderByLikesCount($query, LikeType::LIKE, $direction);
    }
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);
        });
    }
1275672
public function liked($userId=null)
	{
		if(is_null($userId)) {
			$userId = $this->loggedInUserId();
		}
		
		return (bool) $this->likes()
			->where('user_id', '=', $userId)
			->count();
	}
1161155
public function getRelationCountQuery(Builder $query, Builder $parent)
	{
		if ($parent->getQuery()->from == $query->getQuery()->from)
		{
			return $this->getRelationCountQueryForSelfRelation($query, $parent);
		}

		return parent::getRelationCountQuery($query, $parent);
	}
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;
    }
542784
public function select( $query, $bindings = [ ], $useReadPdo = true, $resultClass = null )
	{
		$fetchCustomClass = ! is_null($resultClass) && class_exists($resultClass);

		return $this->run( $query, $bindings, function ( $query, $bindings ) use ( $useReadPdo, $fetchCustomClass, $resultClass ) {
			if( $this->pretending() )
			{
				return [];
			}

			// For select statements, we'll simply execute the query and return an array
			// of the database result set. Each element in the array will be a single
			// row from the database table, and will either be an array or objects.
			$statement = $this->prepared( $this->getPdoForSelect( $useReadPdo )->prepare( $query ), ! $fetchCustomClass );
			$this->bindValues( $statement, $this->prepareBindings( $bindings ) );
			$statement->execute();

			return $fetchCustomClass ? $statement->fetchAll(\PDO::FETCH_CLASS, $resultClass) : $statement->fetchAll();
		});
	}
1265143
public function select($query, $params = [], $type = null) {

		$type = $type === null ? $this -> getEntityNamespace($this -> getTable()) : $type;

		if(null === $this -> getAdapter()) {
			return trigger_error(sprintf('No database adapter was set in "%s"', __METHOD__), E_USER_ERROR);
		}
		
		return new ResultSet($this -> getAdapter() -> query($query, $params) -> toArray(), $type, $this -> getAdapter());
	}
801144
public function scopeUnreadForUser(Builder $query, $userId)
    {
        return $query->has('thread')
            ->where('user_id', '!=', $userId)
            ->whereHas('participants', function (Builder $query) use ($userId) {
                $query->where('user_id', $userId)
                    ->whereNull('deleted_at')
                    ->where(function (Builder $q) {
                        $q->where('last_read', '<', $this->getConnection()->raw($this->getConnection()->getTablePrefix() . $this->getTable() . '.created_at'))
                            ->orWhereNull('last_read');
                    });
            });
    }
97169
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());

        $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondKey);

        if ($this->throughParentSoftDeletes()) {
            $query->whereNull($this->throughParent->getQualifiedDeletedAtColumn());
        }

        $query->getModel()->setTable($hash);

        return $query->select($columns)->whereColumn(
            $parentQuery->getQuery()->from.'.'.$this->localKey, '=', $this->getQualifiedFirstKeyName()
        );
    }