【问题标题】:Laravel 4.2 - pagination failing when using having distance asLaravel 4.2 - 使用距离作为分页失败
【发布时间】: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 .... and distance distance asc limit 12 offset 48)
  • 当然不是。您需要 where 中的逻辑。这件作品在您的select 中。注意这里使用having 是全表扫描。
  • 我知道你在说什么.. 但我不能这样做.. 我必须拆除一个构造查询的函数来注入它,而我目前不能这样做。我会努力投入更多的时间。

标签: laravel laravel-4 pagination eloquent query-builder


【解决方案1】:

如果您的问题没有解决,请使用-&gt;simplePaginate(12) 而不是&gt;paginate(12)

【讨论】:

  • 最好添加paginate()simplePaginate() 之间的区别,并解释为什么simplePaginate() 有效而paginate() 无效。谢谢
猜你喜欢
  • 1970-01-01
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
相关资源
最近更新 更多