title: Haversine distance calculation between two points in Laravel
body: <p>I'm working on a Laravel application in which I need to find all the products within a certain radius of the user's coordinates. Products have a one-to-many relationship with users so that users can have multiple products. I've found that the haversine algorithm can calculate the distance between two points, but I can't seem to make it work.</p>
  <p>I've got the following query.</p>
  <p><strong>Controller</strong></p>
  <pre><code>$latitude = 51.0258761;
  $longitude = 4.4775362;
  $radius = 20000;
  
  $products = Product::with('user')
  -&gt;selectRaw(&quot;*,
              ( 6371 * acos( cos( radians(&quot; . $latitude . &quot;) ) *
              cos( radians(user.latitude) ) *
              cos( radians(user.longitude) - radians(&quot; . $longitude . &quot;) ) + 
              sin( radians(&quot; . $latitude . &quot;) ) *
              sin( radians(user.latitude) ) ) ) 
              AS distance&quot;)
  -&gt;having(&quot;distance&quot;, &quot;&lt;&quot;, $radius)
  -&gt;orderBy(&quot;distance&quot;)
  -&gt;get();
  </code></pre>
  <p>I've set the radius to 20000 for testing purposes, and it appears all products have a distance of 5687,... The problem seems to be that the latitude and longitude of the products are stored in the User table, but I'm not sure how I can access those in my query. I've tried <em>user.latitude</em> and <em>'user-&gt;latitude'</em>, but nothing seems to work.</p>
  <p><strong>Product model</strong></p>
  <pre><code>class Product extends Model
  {
      protected $fillable =
          [
              'soort',
              'hoeveelheid',
              'hoeveelheidSoort',
              'prijsPerStuk',
              'extra',
              'foto',
              'bio'
          ];
  
      public function User()
      {
          return $this-&gt;belongsTo('App\User');
      }
  
      public $timestamps = true;
  }
  </code></pre>
  <p><strong>User model</strong></p>
  <pre><code>use Illuminate\Auth\Authenticatable;
  use Illuminate\Database\Eloquent\Model;
  use Illuminate\Auth\Passwords\CanResetPassword;
  use Illuminate\Foundation\Auth\Access\Authorizable;
  use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
  use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
  
  class User extends Model implements AuthenticatableContract,
                                      AuthorizableContract,
                                      CanResetPasswordContract
  {
      use Authenticatable, Authorizable, CanResetPassword;
  
      protected $table = 'users';
  
      protected $fillable = 
          [
          'firstName', 
          'lastName', 
          'adres',
          'profilepic',
          'description', 
          'longitude',
          'latitude',
          'email', 
          'password'
      ];
  
      protected $hidden = ['password', 'remember_token'];
  
      public function product()
      {
          return $this-&gt;hasMany('App\Product');
      }
  }
  </code></pre>