将SQL查询转换为Laravel雄辩的查询 [英] Converting sql query to laravel eloquent query

查看:54
本文介绍了将SQL查询转换为Laravel雄辩的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

am尝试将以下php核心功能转换为laravel,我已经尝试过了,但无法正常工作

am trying to convert following core php functionality to laravel, i have tried it and its not working

这是我的代码

 public function gmap()
   {
            // Get parameters from URL
    $center_lat = $_GET["lat"];
    $center_lng = $_GET["lng"];
    $radius = $_GET["radius"];
    // Start XML file, create parent node
    $dom = new \DOMDocument("1.0");
    $node = $dom->createElement("markers");
    $parnode = $dom->appendChild($node);

    // Search the rows in the markers table
    $query = ('SELECT id,locationName,locationAddress1,locationLat,locationLong, ( 3959 * acos( cos( radians('$center_lat') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('$center_lng') ) + sin( radians('$center_lat') ) * sin( radians( lat ) ) ) ) AS distance FROM locations HAVING distance < '$radius' ORDER BY distance LIMIT 0 , 20');



    //$result = $con->query($query);

    if (!$result) {
      die("Invalid query: " . mysqli_error());
    }
    //header("Content-type: text/xml");
    // Iterate through the rows, adding XML nodes for each
    while ($row = @mysqli_fetch_assoc($result)){
      $node = $dom->createElement("marker");
      $newnode = $parnode->appendChild($node);
      $newnode->setAttribute("id", $row['id']);
      $newnode->setAttribute("name", $row['locationName']);
      $newnode->setAttribute("address", $row['locationAddress1']);
      $newnode->setAttribute("lat", $row['locationLat']);
      $newnode->setAttribute("lng", $row['locationLong']);
      $newnode->setAttribute("distance", $row['distance']);
    }
    echo $dom->saveXML();
        }

我如何才能将此php功能转换为雄辩的laravel. 但是我不想使用$con,因为它是corephp中的一个连接字符串 请帮助

How can i convert this php functionality to laravel eloquent. However i dont want to use $con , because its a a connection string in corephp Please help

我已经尝试通过以下代码

I have tried by following code

$ok = Location::select('locations.id,ocations.locationName,locations.locationAddress1,locations.locationLat,locations.locationLong,( 3959 * acos( cos( radians('$center_lat') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('$center_lng') ) + sin( radians('$center_lat') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '$radius' ORDER BY distance LIMIT 0 , 20')

推荐答案

您转换后的laravel查询

Your converted laravel query

DB::table('locations')->select(DB::raw('locations.id,locations.locationName,locations.locationAddress1,locations.locationLat,locations.locationLong,( 3959 * acos( cos( radians('%s') ) * cos( radians( locations.locationLat ) ) * cos( radians( locations.locationLong ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( locations.locationLat ) ) ) ) AS distance)->having('distance', '<', '%s')->orderBy('distance', 'ASC')->take(20)->get();

这篇关于将SQL查询转换为Laravel雄辩的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆