【问题标题】:Laravel: Column not found when using "WithCount"Laravel:使用“WithCount”时找不到列
【发布时间】:2021-03-20 11:14:05
【问题描述】:

我正在尝试使用以下优先级订购:

  1. 先显示给定城市的演员
  2. 先显示已给定县的演员表
  3. 显示未提供城市或县的剩余演员表

我的数据库表结构是:

演员表:

id
...
...
shop_id

商店:

id
..
..

位置

id
city
prefecture

可用位置

id
location_id
shop_id
..

为了过滤来自城市/地区的演员,

  1. 首先需要一个店铺
  2. 商店需要available_location
  3. 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_price from casts 内连接 cast_prices 作为cpcp.cast_id = casts.idcp.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_till reservations 中选择*,其中casts.id = reservations.cast_id 和 (status_id = 1 或 status_id = 2)) 和 存在(从attendances 中选择*,其中casts.id = attendances.cast_idattendance_status = 1) 和 is_deleted = 0 并且存在 (select * from cast_prices where casts.id = cast_prices.cast_id order by name asc) 并且 (不存在 (select * 来自reservations 其中casts.id = reservations.cast_id) 或 (从reservations 中选择计数(),其中casts.id = reservations.cast_idstatus_id = 5) is_published = 1 个订单,has_filtered_city desc、has_filtered_prefecture desc、depreciated_price desc 限制 20 偏移量 0)

我在 has_filtered_city 中哪里出错了?它有时可以完美运行,但有时会抛出“未知列:has_fiiltered_city”错误。

【问题讨论】:

  • 错误消息提供了查询文本。它不包含对 has_filtered_cityhas_filtered_prefecture 列的引用,但最终 ORDER BY 除外。
  • 如果我执行以下操作:$casts->first()->has_filtered_city;,它会向我显示值,但它无法按 orderBy 工作。
  • 它没有按顺序工作。 尝试使用位置规范。例如,如果您在输出结构中选择此列作为第 3 列,则 ORDER BY 3 DESC 将按此列排序。
  • 非常感谢。我试试看

标签: php mysql laravel laravel-5 eloquent


【解决方案1】:

在 whereHas(嵌套)中使用 whereHas 会导致无法为 withCount 找到列的问题。在这些场景中,明智的做法是使用 innerJoin 并选择所需的列并使用该列进行过滤。

$casts = $casts->select('casts.*', 'calc_c.cast_id', 'calc_c.max_loc')->join(DB::raw("
            (SELECT casts.id AS cast_id,
                    casts.NAME,
                    casts.shop_id,
                    cal.max_loc
                    FROM casts
                    INNER JOIN (SELECT Min(CASE
                    WHEN al.city = '{$city}' THEN 1
                    WHEN al.prefecture = '{$prefecture}' THEN 2
                    ELSE 3
                    END) AS max_loc,
                    al.shop_id
                    FROM (SELECT available_locations.location_id,
                    available_locations.shop_id,
                    locations.prefecture,
                    locations.city
                    FROM available_locations,
                    locations
                    WHERE available_locations.location_id =
                    locations.id) al
                    GROUP BY al.shop_id) cal
                    ON casts.shop_id = cal.shop_id
                    ORDER BY casts.id ASC) AS calc_c"), 'casts.id', '=', 'calc_c.cast_id');

                $casts = $casts->orderBy('max_loc', 'ASC');

【讨论】:

    猜你喜欢
    • 2020-01-30
    • 2018-07-11
    • 2019-08-06
    • 1970-01-01
    • 2020-05-29
    • 2019-03-27
    • 2023-03-28
    • 2019-11-16
    • 2017-05-07
    相关资源
    最近更新 更多