Reputation: 2987
This is my code:
public function scopeClosest($query, $lat, $lng)
{
return $query->selectRaw(
'Select * , ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance'
);
}
If I run it I get this error:
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Select * ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radian' at line 1
If I remove Select*, so it looks like this:
return $query->selectRaw(
'( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance'
);
no error is produces and I get 5 objects back but they only contain distance like this:
#items: Collection {#377 ▼
#items: array:5 [▼
0 => Listing {#378 ▼
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:1 [▼
"distance" => 5511.8536628578
]
Upvotes: 0
Views: 109
Reputation: 3497
I am not sure but try this:
return $query->selectRaw(
'*, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance'
);
Upvotes: 1