I use Laravel 5.4 and I want to use distance in Eloquent.
In my repository, I have :
public function getRecommended($lat, $lng, $distance, $n)
{
\DB::enableQueryLog();
$raw = \DB::raw('ROUND ( ( 6371 * acos( cos( radians('.$lat.') ) * cos( radians( ad_latitude ) ) * cos( radians( ad_longitude ) - radians('.$lng.') ) + sin( radians('.$lat.') ) * sin( radians( ad_latitude ) ) ) ) ) AS distance');
$data = $this->queryGetAds()->addSelect($raw)->orderBy( 'distance', 'ASC' )->groupBy('distance')->having('distance', '<=', $distance)->paginate($n);
dd(\DB::getQueryLog());
}
The error is :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'group statement'
I checked by removing
->orderBy( 'distance', 'ASC' )->groupBy('distance')->having('distance', '<=', $distance)
And the debugging shows the good query with $raw statement in. I am in a repository.
Is there something missing ?