mercredi 7 février 2018

Trying to add a calculated distance to Eloquent

I am trying to add a distance calculated inside a Model::get() method. The distance should be marked as "distance", while now it returns nothing (no added select).

Here is my Eloquent Query : `

Candidate::whereHas('positions', function($query) use ($data) {
           $query->where('positions.id', $data['position']);
        })
        ->whereHas('user', function($query) use ($data) {
           $query->where('status', '=', 1);
           if($data['lat'] !== null and $data['lng'] !== null){
           $query->whereHas('address', function($query) use ($data) {
           $sqlDistance = DB::raw('( 111.045 * acos( cos( radians(' . $data['lat'] . ') ) * cos( radians( addresses.latitude ) ) 
* cos( radians( addresses.longitude ) - radians(' . $data['lng']  . ') ) 
+ sin( radians(' . $data['lat']  . ') ) * sin( radians( addresses.latitude ) ) ) )');
           $query->selectRaw("{$sqlDistance} AS distance");
          });
         }
         })->with('user.address','user','user.media')
         ->get();

`

Here is what I get :

enter image description here

Any help would be appreciated. Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire