I have a scope in my model which creates an alias and I need to execute a where on it, I understand that MySql dose not allow this.
Standard SQL doesn't allow you to refer to a column alias in a WHERE clause. This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined.
However, I'm wondering if there is a possible laravel work around? My scope creates an alias distance and I want to check the distance.
public static function scopeDistance($query, $lat, $long)
{
return $query->select(array('*',\DB::raw('( 3959 * acos( cos( radians(' . $lat . ') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(' . $long . ') ) + sin( radians(' . $lat .') ) * sin( radians(latitude) ) ) ) AS distance')));
}
In my controller:
\App\Profile::distance($latitude, $longitude)->where('distance', '<', 50);