enter code here
I guess its pretty straightforward, But surely i am missing something here. What I am trying to achieve here is to calculate the nearest location using the current position. Without adding the calculation part the query works fine.
I get an error saying "syntax error, unexpected 'AS' (T_AS), expecting ',' or ')'"
Any help will be appreciated.
enter code here
$testjoin = doctors::where('location_id',$data['location_id'])
->where('medication_id',$data['medication_id'])
->where('specialization_id',$data['specialization_id'])
->where('facility_id',$data['facility_id'])
->where('distance','<=',20)->orderBy('distance','desc')->take('20')
->join('locations', 'doctors.location_id', '=', 'locations.id')
->join('medication_types', 'doctors.medication_id','=','medication_types.id')
->join('specializations', 'doctors.specialization_id','=','specializations.id')
->join('facility_types', 'doctors.facility_id','=','facility_types.id')
->select(
'doctors.id',
'doctors.location_id',
'doctors.first_name',
'doctors.contact',
'doctors.email',
'doctors.lat',
'doctors.lng',
'locations.location',
'medication_types.medication',
'specializations.specialization',
'facility_types.type',
DB::raw( 6371 * acos( cos( radians(15.538210) ) * cos( radians( lat ) ) *
cos( radians( lng ) - radians(73.822974) ) + sin( radians(15.538210) ) *
sin( radians( lat ) ) ) AS distance )
)->get();
I think you cant do calculations in select. Select () only select the fields. Please try change the calculation
( 6371 * acos( cos( radians(15.538210) ) * cos( radians( lat ) ) *
cos( radians( lng ) - radians(73.822974) ) + sin( radians(15.538210) ) *
sin( radians( lat ) ) ) ) AS distance
By
DB:raw( 6371 * acos( cos( radians(15.538210) ) * cos( radians( lat ) ) *
cos( radians( lng ) - radians(73.822974) ) + sin( radians(15.538210) ) *
sin( radians( lat ) ) ) AS distance )
You can use DB:raw to prepare a new column in you selected fields.
And please check "from","having","limit", "orderby" all this could be used as method of the doctors models. For eg.
->where('distance','<=',20)->orderBy('distance','desc')->take('20')->get()
Please try this and let me know how it works :)