Skip to content

Commit 30e0a85

Browse files
committed
Document withCentroid
1 parent 49b9c36 commit 30e0a85

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

API.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ An enum is provided with the following values:
7979
* [whereDisjoint](#whereDisjoint)
8080
* [whereEquals](#whereEquals)
8181
* [whereSrid](#whereSrid)
82+
* [withCentroid](#withCentroid)
8283

8384
### withDistance
8485

@@ -452,3 +453,35 @@ Place::query()
452453
->exists(); // true
453454
```
454455
</details>
456+
457+
### withCentroid
458+
459+
Retrieves the centroid of the geometry object. Uses [ST_Centroid](https://dev.mysql.com/doc/refman/8.0/en/gis-polygon-property-functions.html#function_st-centroid).
460+
461+
| parameter name | type | default |
462+
|---------------------|---------------------|--------------|
463+
| `$column` | `Geometry \ string` | |
464+
| `$alias` | `string` | `'centroid'` |
465+
466+
<details><summary>Example</summary>
467+
468+
```php
469+
$polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}');
470+
Place::create(['polygon' => $polygon]);
471+
472+
$placeWithCentroid = Place::query()
473+
->withCentroid('polygon')
474+
->withCast('centroid', Point:class) // This is important, otherwise the centroid will be returned as a binary string.
475+
->first();
476+
477+
echo $placeWithDistance->centroid; // POINT(0 0)
478+
479+
// when using alias:
480+
$placeWithCentroid = Place::query()
481+
->withCentroid('polygon', 'centroid_alias')
482+
->withCast('centroid_alias', Point:class)
483+
->first();
484+
485+
echo $placeWithDistance->centroid_alias; // POINT(0 0)
486+
```
487+
</details>

0 commit comments

Comments
 (0)