Laravel :query select 'as' 不使用 where 或 have

Laravel :query select 'as' not working with where or have

我正在尝试联系附近的公司 这是我的查询

$lat = 21.41241750000001;
$long = 39.23094140625001;
return
    $this->model->select('id', 'lat', 'long', DB::raw(sprintf(
    '(6371 * acos(cos(radians(%1$.7f)) * cos(radians(`lat`)) * cos(radians(`long`) - radians(%2$.7f)) + sin(radians(%1$.7f)) * sin(radians(`lat`)))) AS `distance`',
    $lat,
    $long
)))
    ->having('distance', '<', 50)
    ->orderBy('distance', 'asc')
    ->paginate();

总是得到我

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'having clause' (SQL: select count(*) as aggregate from companies having distance = 50)

我的 table 架构

+-------------------+----------------------------------------------------+------+-----+---------+----------------+
| Field             | Type                                               | Null | Key | Default | Extra          |
+-------------------+----------------------------------------------------+------+-----+---------+----------------+
| id                | int(10) unsigned                                   | NO   | PRI | NULL    | auto_increment |
| name_en           | varchar(191)                                       | NO   |     | NULL    |                |
| name_ar           | varchar(191)                                       | NO   |     | NULL    |                |
| search_keywords   | text                                               | NO   |     | NULL    |                |
| status            | enum('REJECTED','ACCEPTED','PENDING','DUPLICATED') | NO   | MUL | PENDING |                |
| rejection_reason  | text                                               | NO   |     | NULL    |                |
| industry          | varchar(191)                                       | NO   |     | NULL    |                |
| website           | varchar(191)                                       | NO   |     | NULL    |                |
| city_id           | int(10) unsigned                                   | YES  | MUL | NULL    |                |
| zone_id           | int(10) unsigned                                   | NO   | MUL | NULL    |                |
| size              | int(11)                                            | NO   |     | NULL    |                |
| lat               | varchar(191)                                       | NO   | MUL | NULL    |                |
| long              | varchar(191)                                       | NO   |     | NULL    |                |
| formatted_address | text                                               | NO   |     | NULL    |                |
| street            | varchar(191)                                       | NO   |     | NULL    |                |
| building          | varchar(191)                                       | NO   |     | NULL    |                |
| floor_no          | int(10) unsigned                                   | NO   |     | NULL    |                |
| landmarks         | varchar(255)                                       | NO   |     | NULL    |                |
| company_id        | int(10) unsigned                                   | YES  | MUL | NULL    |                |
| created_at        | timestamp                                          | YES  |     | NULL    |                |
| updated_at        | timestamp                                          | YES  |     | NULL    |                |
+-------------------+----------------------------------------------------+------+-----+---------+----------------+

注意:如果我在没有 have 的情况下进行查询并订购返回和计算的距离 有帮助吗?

我遇到了类似的问题。我根据您的上下文调整了我的代码。您可以尝试添加选择。 我认为这段代码是您解决方案的起点:

$lat = 21.41241750000001;
$long = 39.23094140625001;
Companies::addSelect(['distance' => DB::raw(sprintf('(6371 * acos(cos(radians(%1$.7f)) * cos(radians(`lat`)) * cos(radians(`long`) - radians(%2$.7f)) + sin(radians(%1$.7f)) * sin(radians(`lat`)))) AS `distance`',
                      $lat,
                      $long))
                      ])                    
          ->havingRaw('distance < 50')
          ->orderBy('distance', 'asc')
          ->get(); 

我无法测试它;向我们发送反馈!

我的问题在 paginate 所以我用了 get 但我不知道为什么所以我有人可以解释它会很棒