douxie7339 2016-12-23 13:31
浏览 31

Laravel纬度和经度半径

I am trying to find all postcodes within X radius of {lat}/{long}, however I am receiving a TypeError.

I'm getting this error:

Type error: Argument 1 passed to App\Transformers\PostcodesTransformer::transform() must be an instance of App\Postcodes, instance of stdClass given, called in /home/vagrant/postcode-data/vendor/league/fractal/src/Scope.php on line 338

The query is:

$postcodes = DB::select("Select 
    id, 
    postcode, 
    latitude, 
    longitude, 
    district, 
    postal_town, 
    county, 
    country, 
    northing, 
    easting, 
    type,
    acos(sin(".$lat.")*sin(radians(latitude)) + cos(".$lat.")*cos(radians(latitude))*cos(radians(longitude)-".$lon.")) * ".$R." As distance
        From (
            Select *
            From uk_postcodes
            Where latitude Between ".$minLat." And ".$maxLat."
              And longitude Between ".$minLon." And ".$maxLon."
        ) As FirstCut
        Where acos(sin(".$lat.")*sin(radians(latitude)) + cos(".$lat.")*cos(radians(latitude))*cos(radians(longitude)-".$lon.")) * ".$R."< ".$rad."
        Order by distance");
    return $this->response->withCollection($postcodes, new PostcodesTransformer); 

However the result of the DB::select query returns stdClass.

How would I have it return an instance of the Postcodes class?

  • 写回答

2条回答 默认 最新

  • doudui2229 2016-12-23 13:45
    关注

    According to your code, you aren't using Laravel' Eloquent Model concept to make the queries to the Database.

    Read about: Eloquent: Getting Started.

    So you can have a collection of Postcode objects like this:

    Create a model named Postcode like this:

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Postcode extends Model
    {
        protected $table = 'postcodes';
        protected $guarded = ['id'];
    }
    

    Then while creating your query, you can do it like this, you can use Laravel's query builder's method - selectRaw():

    $postcodes = Postcode::selectRaw("
        *,
        acos(sin(".$lat.")*sin(radians(latitude)) + cos(".$lat.")*cos(radians(latitude))*cos(radians(longitude)-".$lon.")) * ".$R." As distance
            From (
                Select *
                From uk_postcodes
                Where latitude Between ".$minLat." And ".$maxLat."
                  And longitude Between ".$minLon." And ".$maxLon."
            ) As FirstCut
            Where acos(sin(".$lat.")*sin(radians(latitude)) + cos(".$lat.")*cos(radians(latitude))*cos(radians(longitude)-".$lon.")) * ".$R."< ".$rad."
    ")
    ->orderBy('distance', 'asc')
    ->get();
    

    You'll now get a collection of Postcode model Objects instead of StdClass. It would be better if you break your select raw query more deeply, as it seems to me that it will not work!

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用