获取 lat/long + 范围内的所有配置文件(laravel - laravel-mysql-空间)
Get all profiles in lat/long + range (laravel - laravel-mysql-spatial)
我正在尝试在我的 laravel 应用程序中构建一个表单,以从我的城市 table 获取一个 selected 城市或周围 x 公里内的所有用户配置文件。我正在尝试使用 laravel-mysql-spatial,它利用了 ST_DISTANCE_SPHERE mysql 函数,但我无法真正理解 eloquent 的东西需要
基本上每个配置文件都链接到某个具有位置 POINT() 字段的城市,但我希望能够 return 城市周围 x 公里的所有配置文件 select通过表格。
用户模型:
public function profile() {
return $this->hasOne('App\Profile');
}
资料模型:
public function user() {
return $this->belongsTo('App\User');
}
public function city() {
return $this->belongsTo('App\City');
}
城市模式:
protected $spatialFields = [
'location'
];
public function profiles() {
return $this->hasMany('\App\Profile');
}
laravel-mysql-spatial有以下函数来计算两点之间的距离
distanceSphere($point, $point, $range);
但无法真正想出 how/where 将其插入以获得所需的输出
User::with(['reviews', 'profile', 'profile.media','profile.city', 'categories'])->where(['type' => 'mester'])
这是我的 users/profiles
if (!empty($request->get('city_id')))
$users_query->whereHas('profile', function (Builder $query) use ($request) {
$query->where(['city_id' => $request->get('city_id')]);
});
我在select访问一个城市时使用它按城市过滤。
$request->input('range')
我通过范围输入得到这个作为距离
我已经查看过类似的问题,但无法将所有内容连接在一起以获得所需的输出。任何帮助将不胜感激
我认为您可以在 profile.city 上使用 whereHas
而不是个人资料:
$otherCityPoint = City::where('id', $request->city_id)->value('location');
$users_query->whereHas('profile.city', function (Builder $query) use ($request,$otherCityPoint) {
$query->distanceSphere('location', $otherCityPoint, $request->input('range') * 1000); // 3 arg is meters
});
这应该 return 所有拥有配置文件的城市在给定距离内的用户。
我正在尝试在我的 laravel 应用程序中构建一个表单,以从我的城市 table 获取一个 selected 城市或周围 x 公里内的所有用户配置文件。我正在尝试使用 laravel-mysql-spatial,它利用了 ST_DISTANCE_SPHERE mysql 函数,但我无法真正理解 eloquent 的东西需要
基本上每个配置文件都链接到某个具有位置 POINT() 字段的城市,但我希望能够 return 城市周围 x 公里的所有配置文件 select通过表格。
用户模型:
public function profile() {
return $this->hasOne('App\Profile');
}
资料模型:
public function user() {
return $this->belongsTo('App\User');
}
public function city() {
return $this->belongsTo('App\City');
}
城市模式:
protected $spatialFields = [
'location'
];
public function profiles() {
return $this->hasMany('\App\Profile');
}
laravel-mysql-spatial有以下函数来计算两点之间的距离
distanceSphere($point, $point, $range);
但无法真正想出 how/where 将其插入以获得所需的输出
User::with(['reviews', 'profile', 'profile.media','profile.city', 'categories'])->where(['type' => 'mester'])
这是我的 users/profiles
if (!empty($request->get('city_id')))
$users_query->whereHas('profile', function (Builder $query) use ($request) {
$query->where(['city_id' => $request->get('city_id')]);
});
我在select访问一个城市时使用它按城市过滤。
$request->input('range')
我通过范围输入得到这个作为距离
我已经查看过类似的问题,但无法将所有内容连接在一起以获得所需的输出。任何帮助将不胜感激
我认为您可以在 profile.city 上使用 whereHas
而不是个人资料:
$otherCityPoint = City::where('id', $request->city_id)->value('location');
$users_query->whereHas('profile.city', function (Builder $query) use ($request,$otherCityPoint) {
$query->distanceSphere('location', $otherCityPoint, $request->input('range') * 1000); // 3 arg is meters
});
这应该 return 所有拥有配置文件的城市在给定距离内的用户。