Search code examples
databaselaravelpostgresqleloquentpostgis

Query builder join raw (postgres / postgis)


Is it possible to do this query in Laravel query builder?

SELECT table2.id FROM table1
JOIN table2 ON ST_Intersects(table2.geom,table1.geom)
WHERE table1.id = 1234

I have tried to search in Laravel doc (Advanced Join Clauses) but I couldn't find any example that fit my problem.

$query = Table1::query()
    ->select('table2.id')
    ->join(...)
    ->where('table1.id','=',1234)
    ->get();

Solution

  • Ok, I have found the solution:

    $query = Table1::query()
        ->select('table2.id')
        ->join('table2', function ($query) {  
            $query->where(\DB::raw('ST_Intersects(table2.geom,table1.geom)'), true);
        })
        ->where('table1.id','=',1234)
        ->get();