我的代码写在下面
$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:选择count(*)作为event_table
的集合,且距离> 0)“,” exception“:” PDOException:SQLSTATE [42S22]:找不到列:1054 **“具有子句”中的未知列“距离”
当我删除此-> havingRaw('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公里半径。我怎样才能做到这一点??
答案 0 :(得分:0)
您可以在where语句中使用完整表达式,而不必使用例如
。 ->whereRaw("(6371 * ACOS(COS(RADIANS('%s')) * COS(RADIANS(price)) * COS(RADIANS(min_delivery) - RADIANS('%s')) + SIN(RADIANS('%s')) * SIN(RADIANS(min_delivery)))) > 0")