Php Eloquent以UTF8的形式返回点数据
我正在用Elount从数据库中获取一些数据。我的数据库有一列使用Php Eloquent以UTF8的形式返回点数据,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我正在用Elount从数据库中获取一些数据。我的数据库有一列使用点数据类型。这些列的值如点(52.45083-1.930546) 我需要数据库中的这些数据,然后将其作为JSON返回。包括点数据。但是,Elount以以下形式返回: array(7) { ["id"]=> int(2) ["city_id"]=> int(1) ["image"]=> NULL ["name"]=> string(20) "Uni
点
数据类型。这些列的值如点(52.45083-1.930546)
我需要数据库中的这些数据,然后将其作为JSON返回。包括点
数据。但是,Elount以以下形式返回:
array(7) {
["id"]=>
int(2)
["city_id"]=>
int(1)
["image"]=>
NULL
["name"]=>
string(20) "University of London"
["geo_coords"]=>
string(25) "ͯ���I@�h�^`V��"
["created_at"]=>
string(19) "2017-01-11 10:53:46"
["updated_at"]=>
NULL
}
为什么会这样?将以下代码添加到您的模型中
protected $geofields = array('geo_coords');
然后为geo_coords添加setter和getter
二传手
public function setGeoCoordsAttribute($value) {
$this->attributes['geo_coords'] = DB::raw("POINT($value)");
}
public function getGeoCoordsAttribute($value) {
return str_replace(['POINT(', ')', ' '], ['', '', ','], $value);
}
获得者
public function setGeoCoordsAttribute($value) {
$this->attributes['geo_coords'] = DB::raw("POINT($value)");
}
public function getGeoCoordsAttribute($value) {
return str_replace(['POINT(', ')', ' '], ['', '', ','], $value);
}
现在,在查询时,您将把该值作为带有lat和long的简单字符串来点
public function newQuery($excludeDeleted = true)
{
$raw='';
foreach($this->geofields as $column){
$raw .= ' astext('.$column.') as '.$column.' ';
}
return parent::newQuery($excludeDeleted)->addSelect('*',DB::raw($raw));
}
不确定eloquent是否支持点数据。可能是转换模型的好方法。可能有帮助: