Skip to content

orderByDistance() #64

@robjbrain

Description

@robjbrain

I thought i'd put this here for anyone else that wanted it.

I wanted to orderByDistance and it was as simple as:

public function scopeOrderByDistance($query, $geometryColumn, $geometry)
    {
        $this->isColumnAllowed($geometryColumn);

        $query->orderByRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?))", [
            $geometry->toWkt()
        ]);

        return $query;
    }

Presumably you can add these scopes for the other columns as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions