doudi5524
2017-08-01 13:52
浏览 124
已采纳

Laravel DB :: Raw无法按预期工作

I am forming the query below:

SELECT *, ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS('17.4073331') ) + COS( RADIANS( `latitude` ) )* COS( RADIANS('17.4073331')) * COS( RADIANS( `longitude` ) - RADIANS('78.3802307'))) * 6380 AS distance 
FROM bbq_branches_by_city 
HAVING distance <= 20

I am doing like below in laravel:

DB::table('bbq_branches_by_city')
  ->select(DB::raw("*, ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS($lat) ) + COS( RADIANS( `latitude` ) )* COS( RADIANS($lat)) * COS( RADIANS( `longitude` ) - RADIANS($long))) * 6380 AS distance"))
  ->havingRaw("distance <= 20")
  ->get();

Can you let me know where I am mistaking?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douba8048 2017-08-01 14:00
    已采纳

    Try this:

    DB::table('bbq_branches_by_city')
         -> select(["*",DB::raw("ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS($lat) ) + COS( RADIANS( `latitude` ) )* COS( RADIANS($lat)) * COS( RADIANS( `longitude` ) - RADIANS($long))) * 6380 AS distance")])
         ->havingRaw("distance<=20")
         ->get();
    

    Just make the select as array like ['*','distance']

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongzhen4180 2017-08-01 13:59

    You can try below code, based on running-queries:

    $data = DB::select('select *,ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS('17.4073331') ) + COS( RADIANS( `latitude` ) )* COS( RADIANS('17.4073331')) * COS( RADIANS( `longitude` ) - RADIANS('78.3802307'))) * 6380 AS distance from bbq_branches_by_city having distance<=20');
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题