根据 whereHas 查询获取最高和最低数据
Get highest & lowest data based on a whereHas query
基于 apartment finder
所有 types
其中有 apartments
,基于表单搜索正在 shown/returned。这是使用 whereHas
查询完成的。
例如:
A user has different choices; a garden/balcony, price range, floor
etc.
The query is being run based on these choices, and if there are
apartments which meet the search requirements the parent of these
apartments (type) are being returned.
最终我想显示一个类型的living surface
范围(lowest-highest)
,但是每个apartment
的living surface
是不同的。
让 highest
和 lowest
apartment.living_surface
被 whereHas
返回的最有效方法是什么.
$types = Type::where('type', '=', 'studio')->whereHas('apartments', function ($query) use ($request) {
if ($request->view == 'garden') {
$query->where('view', '=', $request->view);
} elseif ($request->view == 'balcony') {
$query->where('view', '=', $request->view);
} elseif ($request->view == 'other') {
$query->where('view', '=', $request->view);
}
if ($request->floor == 'upper') {
$query->where('floor', '<', '5');
} elseif ($request->floor == 'lower') {
$query->where('floor', '>', '4');
}
if ($request->sun == 'morning') {
$query->where('sun', '=', 'morning');
} elseif ($request->sun == 'evening') {
$query->where('sun', '=', 'evening');
}
if ($request->price == '1') {
$query->where('price', '<', '900');
} elseif ($request->price == '2') {
$query->where('price', '<', '999');
} elseif ($request->price == '3') {
$query->where('price', '>', '999');
}
})->with('apartments')->get();
一个解决方案(我认为)是提取 apartments
的所有 ids
和 运行 另一个基于 ids
的查询,但这不会由于 运行 对某事(我希望)的多个查询要在一个查询中完成,因此实际上并没有尽可能优化。
这可以通过 Collection Methods
- Laravel Docs
可以对查询的集合使用 运行 方法 - 在本例中 $types
foreach ($types as $type) {
$type->lowest_living_area = $type->apartments>sortBy('living_area')->first()->living_area);
$type->highest_living_area = $type->apartments->sortBy('living_area')->last()->living_area);
}
基于 apartment finder
所有 types
其中有 apartments
,基于表单搜索正在 shown/returned。这是使用 whereHas
查询完成的。
例如:
A user has different choices; a garden/balcony, price range, floor etc.
The query is being run based on these choices, and if there are apartments which meet the search requirements the parent of these apartments (type) are being returned.
最终我想显示一个类型的living surface
范围(lowest-highest)
,但是每个apartment
的living surface
是不同的。
让 highest
和 lowest
apartment.living_surface
被 whereHas
返回的最有效方法是什么.
$types = Type::where('type', '=', 'studio')->whereHas('apartments', function ($query) use ($request) {
if ($request->view == 'garden') {
$query->where('view', '=', $request->view);
} elseif ($request->view == 'balcony') {
$query->where('view', '=', $request->view);
} elseif ($request->view == 'other') {
$query->where('view', '=', $request->view);
}
if ($request->floor == 'upper') {
$query->where('floor', '<', '5');
} elseif ($request->floor == 'lower') {
$query->where('floor', '>', '4');
}
if ($request->sun == 'morning') {
$query->where('sun', '=', 'morning');
} elseif ($request->sun == 'evening') {
$query->where('sun', '=', 'evening');
}
if ($request->price == '1') {
$query->where('price', '<', '900');
} elseif ($request->price == '2') {
$query->where('price', '<', '999');
} elseif ($request->price == '3') {
$query->where('price', '>', '999');
}
})->with('apartments')->get();
一个解决方案(我认为)是提取 apartments
的所有 ids
和 运行 另一个基于 ids
的查询,但这不会由于 运行 对某事(我希望)的多个查询要在一个查询中完成,因此实际上并没有尽可能优化。
这可以通过 Collection Methods
- Laravel Docs
可以对查询的集合使用 运行 方法 - 在本例中 $types
foreach ($types as $type) {
$type->lowest_living_area = $type->apartments>sortBy('living_area')->first()->living_area);
$type->highest_living_area = $type->apartments->sortBy('living_area')->last()->living_area);
}