How to use paginate() with a having() clause when column does not exist in table

前端 未结 7 1877
不知归路
不知归路 2020-12-01 06:41

I have a tricky case ...

Following database query does not work:

DB::table(\'posts\')
->select(\'posts.*\', DB::raw($haversineSQL . \' as distance         


        
7条回答
  •  清歌不尽
    2020-12-01 07:22

    You can calculate the distance in the WHERE part:

    DB::table('posts')
        ->whereRaw($haversineSQL . '<= ?', [$distance])
        ->paginate(10);
    

    If you need the distance value in your application, you'll have to calculate it twice:

    DB::table('posts')
        ->select('posts.*', DB::raw($haversineSQL . ' as distance'))
        ->whereRaw($haversineSQL . '<= ?', [$distance])
        ->paginate(10);
    

提交回复
热议问题