【问题标题】:LARAVEL column Eloquent query with OrderByRawLARAVEL 列雄辩的查询与 OrderByRaw
【发布时间】:2020-06-16 15:10:34
【问题描述】:

我需要用纬度,经度计算距离并按最近的顺序排列,
但是在我的 OrderBy 子句中缺少纬度,我不知道为什么

  ->with(['foglio' => function ($query) use ($latitude, $longitude) {
    $query->select([
      'id',
      'indirizzo',
      'piano',
      'categoria',
      'consistenza',
      'latitude',
      'longitude',
    ]);
    $query->WhereNotNull(['indirizzo', 'latitude', 'longitude']);
  }])
  ->limit($limit)
  ->orderBy(DB::raw('ABS(latitude - ' . $latitude . ') + ABS(longitude - ' . $longitude . ')'), 'ASC')
  ->get();

【问题讨论】:

    标签: laravel eloquent sql-order-by


    【解决方案1】:

    你可以使用:

    ->orderByRaw('ABS(latitude - ' . $latitude .') + ABS(longitude - ' . $longitude .') ASC')

    【讨论】:

    • 未知纬度列
    【解决方案2】:

    您应该使用left join,而不是eager loading
    急切加载将产生两个单独的查询,这就是您无法访问纬度的原因 按子句排序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-26
      • 2021-07-23
      • 2017-11-11
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多