如何将哈弗斯公式放入 laravel 4 查询中
How to put harvesian formulae in laravel 4 query
我有以下代码。我想根据距离排序。 jobs table 中的距离列是 lat,lng 我将从 GET 参数中获取两个变量,假设 $lat,$lng。
我想根据最短距离进行排序。
在 mysql 中获得最短距离的代码是这样的。
latpoint 和 longpoint 将来自 $_GET
SELECT lat, lng,
111.045* DEGREES(ACOS(COS(RADIANS(latpoint))
* COS(RADIANS(latitude))
* COS(RADIANS(longpoint) - RADIANS(longitude))
+ SIN(RADIANS(latpoint))
* SIN(RADIANS(latitude)))) AS distance_in_km
来自职位
按 distance_in_km 订购
限制 15
Job::with(array("job_categories" => function($query) {
$query -> with('cat_detail');
})) -> whereHas('job_categories', function($query) use ($cats_filter_value) {
//$query->where('category_id',13);
if (!empty($cats_filter_value)) {
$query -> whereIn('category_id', $cats_filter_value);
}
}) -> with(array("job_skills" => function($query) {
$query -> with('skill_detail');
})) -> whereHas('job_skills', function($query) use ($skill_filter_value) {
//$query->where('category_id',13);
if (!empty($skill_filter_value)) {
$query -> whereIn('skills_id', $skill_filter_value);
}
}) -> with("job_bids") -> with('job_follow') -> with("job_watch") -> with(array("job_award_me" => function($query) use ($conditions) {
$query -> with('job_award_detail');
})) -> with("job_to_user") -> orderBy($a, $c) -> where(function($query) use ($conditions) {
if (isset($conditions['user_id']) || isset($conditions['user_to_id'])) {
//$query->orWhere('job_status','=',$conditions['user_id']);
} else {
$query -> where('job_status', $conditions['job_status']);
}
if (isset($conditions["job_awarded"])) {
if (isset($conditions['user_id']) || isset($conditions['user_to_id'])) {
} else {
$query -> where('job_awarded', $conditions['job_awarded']);
}
}
if (isset($conditions["budget"])) {
$query -> where('budget', ">", $conditions['budget']['start']);
$query -> where('budget', "<=", $conditions['budget']['end']);
}
if (isset($conditions['job_filter_value'])) {
//
foreach ($conditions['job_filter_value'] as $key => $value) {
$query -> where('job_type', 'LIKE', '%' . $value . '%');
}
}
if (isset($conditions['rating'])) {
$query -> where('rating', '<=', $conditions['rating']);
}
if (isset($conditions['job_title'])) {
$query -> where('job_title', 'LIKE', '%' . $conditions['job_title'] . '%');
}
if (isset($conditions['user_id'])) {
$query -> where('user_id', '=', $conditions['user_id']);
}
if (isset($conditions['main_type'])) {
$query -> where('is_shoot', '=', $conditions['main_type']);
$query -> whereIn('job_status', array(2, 3)) -> whereIn('job_awarded', array(0, 1));
}
if (isset($conditions['token'])) {
//foreach ($conditions['token'] as $key => $value) {
//$c = implode(',', $conditions['token']);
$query -> whereIn('job_token', $conditions['token']);
//}
}
if (isset($conditions['location'])) {
//foreach ($conditions['token'] as $key => $value) {
//$c = implode(',', $conditions['token']);
$query -> whereIn('user_id', $conditions['location']);
//}
}
if (isset($conditions['job_to_ids'])) {
$query -> whereIn('id', $conditions['job_to_ids']);
//$query -> orWhere('id', '=', $value);
}
}) -> paginate($records);
我必须在 laravel 查询中按距离排序。
谢谢
您可以尝试 orderBy()
作为:
->orderBy(DB::raw('111.045* DEGREES(ACOS(COS(RADIANS('.$latpoint.')) * COS(RADIANS(lat)) * COS(RADIANS('.$longpoint.') - RADIANS(lng)) + SIN(RADIANS('.$latpoint.')) * SIN(RADIANS(lat))))'));
我有以下代码。我想根据距离排序。 jobs table 中的距离列是 lat,lng 我将从 GET 参数中获取两个变量,假设 $lat,$lng。 我想根据最短距离进行排序。 在 mysql 中获得最短距离的代码是这样的。 latpoint 和 longpoint 将来自 $_GET
SELECT lat, lng,
111.045* DEGREES(ACOS(COS(RADIANS(latpoint))
* COS(RADIANS(latitude))
* COS(RADIANS(longpoint) - RADIANS(longitude))
+ SIN(RADIANS(latpoint))
* SIN(RADIANS(latitude)))) AS distance_in_km
来自职位 按 distance_in_km 订购 限制 15
Job::with(array("job_categories" => function($query) {
$query -> with('cat_detail');
})) -> whereHas('job_categories', function($query) use ($cats_filter_value) {
//$query->where('category_id',13);
if (!empty($cats_filter_value)) {
$query -> whereIn('category_id', $cats_filter_value);
}
}) -> with(array("job_skills" => function($query) {
$query -> with('skill_detail');
})) -> whereHas('job_skills', function($query) use ($skill_filter_value) {
//$query->where('category_id',13);
if (!empty($skill_filter_value)) {
$query -> whereIn('skills_id', $skill_filter_value);
}
}) -> with("job_bids") -> with('job_follow') -> with("job_watch") -> with(array("job_award_me" => function($query) use ($conditions) {
$query -> with('job_award_detail');
})) -> with("job_to_user") -> orderBy($a, $c) -> where(function($query) use ($conditions) {
if (isset($conditions['user_id']) || isset($conditions['user_to_id'])) {
//$query->orWhere('job_status','=',$conditions['user_id']);
} else {
$query -> where('job_status', $conditions['job_status']);
}
if (isset($conditions["job_awarded"])) {
if (isset($conditions['user_id']) || isset($conditions['user_to_id'])) {
} else {
$query -> where('job_awarded', $conditions['job_awarded']);
}
}
if (isset($conditions["budget"])) {
$query -> where('budget', ">", $conditions['budget']['start']);
$query -> where('budget', "<=", $conditions['budget']['end']);
}
if (isset($conditions['job_filter_value'])) {
//
foreach ($conditions['job_filter_value'] as $key => $value) {
$query -> where('job_type', 'LIKE', '%' . $value . '%');
}
}
if (isset($conditions['rating'])) {
$query -> where('rating', '<=', $conditions['rating']);
}
if (isset($conditions['job_title'])) {
$query -> where('job_title', 'LIKE', '%' . $conditions['job_title'] . '%');
}
if (isset($conditions['user_id'])) {
$query -> where('user_id', '=', $conditions['user_id']);
}
if (isset($conditions['main_type'])) {
$query -> where('is_shoot', '=', $conditions['main_type']);
$query -> whereIn('job_status', array(2, 3)) -> whereIn('job_awarded', array(0, 1));
}
if (isset($conditions['token'])) {
//foreach ($conditions['token'] as $key => $value) {
//$c = implode(',', $conditions['token']);
$query -> whereIn('job_token', $conditions['token']);
//}
}
if (isset($conditions['location'])) {
//foreach ($conditions['token'] as $key => $value) {
//$c = implode(',', $conditions['token']);
$query -> whereIn('user_id', $conditions['location']);
//}
}
if (isset($conditions['job_to_ids'])) {
$query -> whereIn('id', $conditions['job_to_ids']);
//$query -> orWhere('id', '=', $value);
}
}) -> paginate($records);
我必须在 laravel 查询中按距离排序。 谢谢
您可以尝试 orderBy()
作为:
->orderBy(DB::raw('111.045* DEGREES(ACOS(COS(RADIANS('.$latpoint.')) * COS(RADIANS(lat)) * COS(RADIANS('.$longpoint.') - RADIANS(lng)) + SIN(RADIANS('.$latpoint.')) * SIN(RADIANS(lat))))'));