Tuesday, March 14, 2017

Searching Geolocation using Scopes returns syntax error

I have a model in my Laravel application called Business which contains business_name, lat and long columns. Using Google Maps API, I receive locations that I would like to search with specific lat, long against rows in my database.

I am using the following scope which I found here:

public function ScopeDistance($query,$from_latitude,$from_longitude,$distance) { $raw = \DB::raw('ROUND ( ( 3959 * acos( cos( radians('.$from_latitude.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$from_longitude.') ) + sin( radians('.$from_latitude.') ) * sin( radians( latitude ) ) ) ) ) AS distance'); return $query->select('*')->addSelect($raw)->orderBy( 'distance', 'ASC' )->groupBy('distance')->having('distance', '<=', $distance); }

I receive the following error when executing the query:

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'site.businesses.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select *, ROUND ( ( 3959 * acos( cos( radians(52.4408809) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-1.9385408) ) + sin( radians(52.4408809) ) * sin( radians( latitude ) ) ) ) ) AS distance from businesses group by distance having distance <= 20 order by distance asc)

Could someone advise what is wrong here and how I can fix?



via Imran

Advertisement