Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing MySQL Spatial functions using Laravel Eloquent ORM

Query 1:

"SELECT * from search_table WHERE column>=4"

Eloquent ORM Implementation of 'Query1'

$searchResults = SearchTempTable::select(*);

$searchResults = $searchResults->where('column', '>=', 4);

Query 2:

"SELECT * FROM search_table WHERE ST_Intersects(column, geomfromtext( 'POLYGON(($point1X $point1Y, $point2X $point2Y, $point3X $point3Y,$point4X $point4Y,$point1X $point1Y))'))"

How can 'Query 2' be implemented in Eloquent ORM?

like image 260
Nimmy Alice Mathew Avatar asked Dec 29 '25 10:12

Nimmy Alice Mathew


1 Answers

Only raw statement will work for this.

$bindings = [$point1X, $point1Y, ... ];

SearchTempTable::whereRaw(
 "ST_Intersects(column, geomfromtext( 'POLYGON((? ?, ? ?, ? ?, ? ?, ? ?))' ))",
  $bindings
)->get();
like image 111
Jarek Tkaczyk Avatar answered Dec 31 '25 00:12

Jarek Tkaczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!