2017-10-06 6 views
1

내가 같은 테이블을 만드는 오전에 MySQL의 POINT 필드를 처리하는 방법 : 는 Laravel

/** 
* Run the migrations. 
* 
* @return void 
*/ 
public function up() 
{ 

    Schema::create('places', function (Blueprint $table) { 
     $table->engine = 'MyISAM'; 

     $table->increments('id'); 
     $table->text('description'); 

     $table->longText('address'); 
     $table->point('coordinates'); 
     $table->timestamps(); 
    }); 
} 

내가 사용하여 내 데이터베이스에 직접 필드를 생성 :

다음
INSERT INTO `places` (`id`, `description`, `address`, `coordinates`, `created_at`, `updated_at`) 
VALUES 
    (1, 'Plaza Condesa', 'Av. Juan Escutia 4, Hipodromo Condesa, Hipódromo, 06140 Cuauhtémoc, CDMX', X'000000000101000000965B5A0D89693340CC1B711214CB58C0', NULL, NULL); 

내가 사용 Laravel에서 검색 :

MyModel::first() 

모든 값은 내가 얻을 coordinates 필드를 제외하고 제대로 것 같다 다음과 같이 입력하십시오 :

�[Z 
�[email protected]�q�X� 

어떻게 Lolavel을 사용하여 POINT 필드를 얻을 수 있습니까?

+0

나는 그렇지 않았다. – awavi

+0

포인트가 – awavi

+0

입니다. SQL 문으로 내보냈지만 POINT (19.xxx -99.xxx) – awavi

답변

1

현재 데이터베이스에있는 데이터 만 있습니다. Schema::create은 데이터베이스에 테이블을 만들었고 순수 SQL 삽입 문을 수행 한 것보다 낫습니다. 당신은 문자열 또는 정수를 저장하지 않은

, 당신은 소수점 데이터 유형
https://dev.mysql.com/doc/refman/5.7/en/gis-class-point.html

다음이 데이터를 얻을 수 Laravel 설득력을 사용을 사용하지만,보기의 웅변 점에서 일부 있어 데이터를 반향 시키면 게시 한 것처럼 보입니다.

원하는 것은 이진을 원하는 형식으로 변환하는 모델 클래스의 일부 논리입니다. Laravel model with POINT/POLYGON etc. using DB::raw expressions

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 
use Illuminate\Support\Facades\DB; 

class Places extends Model 
{ 
    protected $geometry = ['coordinates']; 

    /** 
    * Select geometrical attributes as text from database. 
    * 
    * @var bool 
    */ 
    protected $geometryAsText = true; 

    /** 
    * Get a new query builder for the model's table. 
    * Manipulate in case we need to convert geometrical fields to text. 
    * 
    * @param bool $excludeDeleted 
    * 
    * @return \Illuminate\Database\Eloquent\Builder 
    */ 
    public function newQuery($excludeDeleted = true) 
    { 
     if (!empty($this->geometry) && $this->geometryAsText === true) 
     { 
      $raw = ''; 
      foreach ($this->geometry as $column) 
      { 
       $raw .= 'AsText(`' . $this->table . '`.`' . $column . '`) as `' . $column . '`, '; 
      } 
      $raw = substr($raw, 0, -2); 

      return parent::newQuery($excludeDeleted)->addSelect('*', DB::raw($raw)); 
     } 

     return parent::newQuery($excludeDeleted); 
    } 
} 

지금 당신은 예를 들어, 수행 할 수 있습니다

, 당신의 상황에 맞게 DB에서 결과 AsText를로드하는 다음 포스트 적응 예를 형성한다 echo Places::first()->coordinates이고 결과는 POINT(19.4122475 -99.1731001)과 같습니다.

당신이하고자하는 일에 따라 Eloquent Events도 볼 수 있습니다. https://laravel.com/docs/5.5/eloquent#events 여기에서 원하는대로 사물을보다 정확하게 변경할 수 있습니다.