如何在Laravel 4模型中为POINT数据列创建访问器? [英] How do I create an Accessor for a POINT data column in a Laravel 4 model?

查看:84
本文介绍了如何在Laravel 4模型中为POINT数据列创建访问器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个user_sessions表,该表具有一个名为"geo_location"的列,这是一个POINT列,用于存储用户当前位置的纬度和经度值;如果不可用,则为NULL.

I've got a user_sessions table that has a column named "geo_location", it's a POINT column that stores the latitude and longitude values for a user's current location, or NULL if it's not available.

当我在Laravel中创建绑定到该表的模型时,仅当geo_location字段完全隐藏时才起作用.否则,它会抛出JSON错误,因为它没有正确查询geo_location列中单独的X和Y值.

When I create a Model in Laravel that binds to that table it only works when the geo_location field is hidden completely. Otherwise it throws a JSON error because it's not properly querying for the separate X and Y values in the geo_location column.

有没有一种方法可以在Laravel模型中创建一个访问器,该访问器可以在数据显示之前对其进行处理,以便可以将其包括在结果中?

Is there a way that I can create an Accessor in my Laravel Model that can manipulate the data before it gets displayed so that I can include it in my results?

我是否需要修改UserSessions控制器并添加get()函数以仅使用原始SQL?

Do I need to modify my UserSessions controller and add a get() function to just use raw SQL instead?

推荐答案

如果您使用的是PostGreSQL + PostGIS,这就是我针对L4.1所做的事情

If you are using PostGreSQL + PostGIS, this is how I did it for L4.1

位置类型为geometry(POINT),是使用迁移表中的原始sql查询创建的

location is of type geometry(POINT), created using a raw sql query in the migration table

表格:

 DB::statement("ALTER TABLE places ADD COLUMN location GEOMETRY(POINT, 4326)");

型号:

 class Place extends Eloquent{
     //mass assignment of fillable field
     #fillable
     protected $fillable = array('name', 'attribute', 'location');
     // if you want to include your POINT data as a JSON attribute
     #append
     protected $append = array('location');
     // the DB:raw statement is particular to PostGreSQL + PostGIS, a native function of PostGIS
     // basically, find the id of the referred place
     // and run the PostGIS function that turns the geometry type to wkt text.
     #accessor
     public function getLocationAttribute(){
         $id =  $this->attributes['id'];
         $wkt = DB::table('places')->find( $id, array(DB::raw('ST_AsText(location) AS location')));
         $location = $wkt->location;
         return $location;
     }

 }

使用REST的示例输出如下:

a sample output using REST looks like this:

{domain}/place/{1}

{domain}/place/{1}

 {
  id: 1,
  name: "Yellowstone",
  created_at: "2014-05-19 08:19:51",
  updated_at: "2014-05-19 08:19:51",
  location: "POINT(121.1 14.4)"
 }

注意:

使用默认访问器

  $this->attributes['location']

返回一个十六进制对象,而不是一个字符串.所以我选择将原始查询与PostGIS函数一起使用.

returns a Hexadecimal Object, rather than a string. so i opted to use a raw query with a PostGIS function.

 {
   id: 1,
   name: "Yellowstone",
   created_at: "2014-05-19 08:19:51",
   updated_at: "2014-05-19 08:19:51",
   location: "0101000020E61000006666666666465E40CDCCCCCCCCCC2C40"
 }

我希望从WKT,可以使用本机php脚本轻松返回经度/纬度. :-)

From the WKT, i hope you can easily return the longitude / latitude using native php scripts. :-)

我希望这会让您对如何创建访问器有所了解.

I hope this gives you an idea on how to create the accessor.

这篇关于如何在Laravel 4模型中为POINT数据列创建访问器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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