【发布时间】:2015-04-15 06:18:08
【问题描述】:
我构建了一个复杂的搜索查询,在引入距离计算之前效果很好。
我就是这样做的..
$profiles = Profile:: ... ... ...
...
...
...
...
$lat = Auth::User()->profile->latitude;
$lng = Auth::User()->profile->longitude;
$gr_circle_radius = 6371;
/*
* Generate the select field for disctance
*/
$disctance_select = sprintf(
"*, ( %d * acos( cos( radians(%s) ) " .
" * cos( radians( latitude ) ) " .
" * cos( radians( longitude ) - radians(%s) ) " .
" + sin( radians(%s) ) * sin( radians( latitude ) ) " .
") " .
") " .
"AS distance",
$gr_circle_radius,
$lat,
$lng,
$lat
);
$profiles = $profiles->select( DB::raw($disctance_select) )
->having( 'distance', '<', 80 )
->orderBy( 'distance', 'ASC' )
->paginate(12);
当我使用 paginate() 时出现此错误
Column not found: 1054 Unknown column 'distance' in 'having clause'
如果我使用 get() 查询有效,但我得到了
Call to undefined method Illuminate\Database\Eloquent\Collection::links()
.. 我真的很喜欢 o 有简单的分页工作。
关于如何解决这个问题有什么好的建议吗?我一直在四处寻找,没有发现任何有用的东西。
显然在使用have时,eloquent有问题?
所有帮助将不胜感激。
【问题讨论】:
-
您应该将
having约束移至where子句。是的,我知道distance列不存在。没关系,只要把你的逻辑移到那里就行了。 -
它不起作用... SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'距离'(SQL:select *,(6371 * acos(cos(弧度) (51.503363) ) * cos( 弧度( 纬度 ) ) * cos( 弧度( 经度 ) - 弧度(-0.127625) ) + sin( 弧度(51.503363) ) * sin( 弧度( 纬度 ) ) ) 与
profiles的距离其中(country_id= 41 and .... anddistancedistance asc limit 12 offset 48) -
当然不是。您需要
where中的逻辑。这件作品在您的select中。注意这里使用having是全表扫描。 -
我知道你在说什么.. 但我不能这样做.. 我必须拆除一个构造查询的函数来注入它,而我目前不能这样做。我会努力投入更多的时间。
标签: laravel laravel-4 pagination eloquent query-builder