【发布时间】:2021-03-20 11:14:05
【问题描述】:
我正在尝试使用以下优先级订购:
- 先显示给定城市的演员
- 先显示已给定县的演员表
- 显示未提供城市或县的剩余演员表
我的数据库表结构是:
演员表:
id
...
...
shop_id
商店:
id
..
..
位置
id
city
prefecture
可用位置
id
location_id
shop_id
..
为了过滤来自城市/地区的演员,
- 首先需要一个店铺
- 商店需要available_location
- Available_location 然后从位置所在的城市或地区中过滤出来
对于那个场景,我使用了以下条件:
$casts = Cast::filterNonBusy()->attendancePresent()->where('is_deleted', 0)->with('review', 'castImage', 'shop', 'shop.location');
if (isset($request->city) || isset($request->prefecture)) {
$city = $request->city;
$prefecture = $request->prefecture;
$locationHavingCityIds = Location::where('city', $city)->pluck('id')->toArray();
$locationHavingPrefectureIds = Location::where('prefecture', $prefecture)->pluck('id')->toArray();
if (!empty($locationHavingCityIds)) {
$casts = $casts->withCount(['shop', 'shop AS has_filtered_city' => function ($shopQuery) use ($locationHavingCityIds) {
return $shopQuery->whereHas('availableLocations', function ($availableLocationQuery) use ($locationHavingCityIds) {
$availableLocationQuery->whereIn('location_id', $locationHavingCityIds);
});
}])->orderBy('has_filtered_city', 'DESC');
}
if (!empty($locationHavingPrefectureIds)) {
$casts = $casts->withCount(['shop', 'shop AS has_filtered_prefecture' => function ($shopQuery) use ($locationHavingPrefectureIds) {
return $shopQuery->whereHas('availableLocations', function ($availableLocationQuery) use ($locationHavingPrefectureIds) {
$availableLocationQuery->whereIn('location_id', $locationHavingPrefectureIds);
});
}])->orderBy('has_filtered_prefecture', 'DESC');
}
}
如果演员有相关城市,它会给我 has_filtered_city 为 1。 否则它给我 has_filtered_city 为 0
而且我是按照has_filtered_city排序的。
当演员没有关联城市时出现问题,它给了我一个错误:
SQLSTATE[42S22]:未找到列:1054 未知列 '订单子句'中的'has_filtered_city'(SQL:选择
casts.,cp.cast_id,cp.depreciated_pricefromcasts内连接cast_prices作为cp上cp.cast_id=casts.id和cp.id= (从 cast_prices cp1 中选择 id WHERE cp1.cast_id = casts.id ORDER BY depreciated_price DESC LIMIT 1) 其中is_published= 1 和 (busy_till为 null 或busy_tillreservations 中选择*,其中casts.id=reservations.cast_id和 (status_id= 1 或status_id= 2)) 和 存在(从attendances中选择*,其中casts.id=attendances.cast_id和attendance_status= 1) 和is_deleted= 0 并且存在 (select * fromcast_priceswherecasts.id=cast_prices.cast_idorder bynameasc) 并且 (不存在 (select * 来自reservations其中casts.id=reservations.cast_id) 或 (从reservations中选择计数(),其中casts.id=reservations.cast_id和status_id= 5) is_published = 1 个订单,has_filtered_citydesc、has_filtered_prefecturedesc、depreciated_pricedesc 限制 20 偏移量 0)
我在 has_filtered_city 中哪里出错了?它有时可以完美运行,但有时会抛出“未知列:has_fiiltered_city”错误。
【问题讨论】:
-
错误消息提供了查询文本。它不包含对
has_filtered_city和has_filtered_prefecture列的引用,但最终 ORDER BY 除外。 -
如果我执行以下操作:
$casts->first()->has_filtered_city;,它会向我显示值,但它无法按 orderBy 工作。 -
它没有按顺序工作。 尝试使用位置规范。例如,如果您在输出结构中选择此列作为第 3 列,则
ORDER BY 3 DESC将按此列排序。 -
非常感谢。我试试看
标签: php mysql laravel laravel-5 eloquent