【发布时间】: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块中相同的表达式