【问题标题】:havingRaw can't work in pagination in laravelhaveRaw 不能在 laravel 的分页中工作
【发布时间】:2019-01-21 01:39:46
【问题描述】:

我的代码写在下面

 $meetings = Event::select(\DB::raw("*, " .
                "( " .
                "6371 * " .
                "acos( " .
                "cos( radians('%s') ) * " .
                "cos( radians( lattitude ) ) * " .
                "cos( " .
                "radians( longitude ) - radians('%s')" .
                ") + " .
                "sin( radians('%s') ) * " .
                "sin( radians( lattitude ) ) " .
                ")" .
                ") AS distance"))
                ->havingRaw('distance > 0')
                ->setBindings([ $lat, $lng, $lat,$radius ], 'select')
                ->paginate(4)->toArray();

当我运行这段代码时,我发现了这个错误

**'有子句'中的未知列'距离'(SQL:从event_table中选择计数(*)作为聚合,距离> 0)“,”异常“:”PDOException:SQLSTATE [42S22]:列没有发现:1054 **中的“有子句”中的未知列“距离”


当我删除这个 ->haveRaw('distance > 0') 行时,这段代码的输出是

"data" => array:4 [
0 => array:19 [
  "event_id" => 14
  "event_name" => "Test Event"
  "event_description" => "this is a test event"
  "event_live_url" => "http://www.facebook.com/event/test-event"
  "address" => "Mirpur, Dhaka, Bangladesh"
  "lattitude" => 23.810301
  "longitude" => 90.412498
  "creator_id" => 6
  "start_time" => "2017-08-29 18:42:58"
  "end_time" => "2017-08-29 00:00:00"
  "timezone" => null
  "event_phone" => "8801705092993"
  "event_email" => "rhythm.shahriar.bd@gmail.com"
  "event_facebook_page" => "http://www.facebook.com/event/test-event"
  "price" => 200.0
  "active" => 1
  "event_category_id" => 1
  "create_at" => "2017-08-29 18:42:58"
  "distance" => 10049.507140444
]

现在我无法应用逻辑来找到该距离内的 1 公里半径。我怎样才能做到这一点??

【问题讨论】:

  • 您不能在有块的内部使用 select 的别名。您必须在 select 中使用完整的表达式
  • 我不明白。你能解释一下吗?
  • having 块中,现在您使用在select 块中创建的distance 别名。这个不允许。在having 块中,您必须运行与select 块中相同的表达式

标签: php laravel


【解决方案1】:

您可以在 where 语句中使用完整的表达式,而不是使用 eg。

->whereRaw("(6371 * ACOS(COS(RADIANS('%s')) * COS(RADIANS(price)) * COS(RADIANS(min_delivery) - RADIANS('%s')) + SIN(RADIANS('%s')) * SIN(RADIANS(min_delivery)))) > 0")

【讨论】:

    猜你喜欢
    • 2021-10-05
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 2015-08-10
    相关资源
    最近更新 更多