title: How to use raw SELECT and WHERE on aliased column with Laravel?
body: <p>I'm trying to use raw select with Laravel Eloquent. But unfortunately I cannot use WHERE condition for aliased "SELECT AS" field (distance).</p>
  
  <p>I get "Column not found: 1054 Unknown column 'distance' in 'where clause'" error.</p>
  
  <p>How can I use "distance" as a condition with Eloquent?</p>
  
  <p>Here is my code</p>
  
  <pre><code>            $firebaseUsers = FirebaseUser::when( (!empty($distance)) , function ($query) use ($distance, $user) {
                  return $query-&gt;select('firebase_users.*', DB::raw("ST_Distance_Sphere( POINT(".$user-&gt;latitude.", ".$user-&gt;longitude."), POINT(latitude, longitude) ) as distance") )
                      -&gt;whereNotNull('latitude')
                      -&gt;whereNotNull('longitude')
                      -&gt;whereRaw('distance &lt;= ?',  [$distance * 1000]);
              })
              -&gt;where('firebase_id', '!=', $user-&gt;firebase_id)
              -&gt;orderByRaw( "FIELD(paid_status, 'yes', 'no')" )
              -&gt;orderBy('last_online', 'DESC')
              -&gt;paginate(30);
  </code></pre>