[英]Eloquent query with subquery
三天以來我一直在解決以下問題:
protected function getTableQuery(): Builder
{
return User::
where('age','>',21)
->AppendDistanceTo(
$this->city->geo->long,
$this->city->geo->lat,
**lo**,
**la**,
);
}
Model 用戶具有 user_id 和城市 city_id 的 ID。
在 model City 中,每個 city_id 都有經度和緯度。 如何從相關的 Model 獲取上述查詢中的經度和緯度 für lo和la ?
非常感謝您的幫助!
我想顯示與所選城市的距離($this->city->geo->long,$this->city->geo->lat)
到用戶居住的城市。
鑒於您的User
model 具有城市關系,而City
model 具有geo
關系,您可以像這樣預先加載城市關系:
User::where('age', '>', 21)
->with('city.geo')
然后, $user->city->geo->long
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.