wildwestriverrider

Experience

14,370

0 Best Reply Awards

  • Member Since 2 Years Ago
  • 143 Lessons Completed
  • 0 Favorites

2nd December, 2017

wildwestriverrider left a reply on 5.5 Point Type Column • 9 months ago

With a simple Point class

class Point
{
    public $x;
    public $y;

    public function __construct($x = null, $y = null)
    {
        $this->x = is_numeric($x) ? floatval($x) : $x;
        $this->y = is_numeric($y) ? floatval($y) : $y;
    }
}

wildwestriverrider left a reply on 5.5 Point Type Column • 9 months ago

This is what I am doing for now.

trait HasPosition
{
    /**
     * @param Point $point
     */
    public function setPositionAttribute(Point $point)
    {
        $query = "GeomFromText('POINT($point->x $point->y)')";
        $this->attributes['position'] = \DB::raw($query);
    }

    /**
     * @param string $value
     * @return Point
     */
    public function getPositionAttribute($value)
    {
        // cleanup the database response into a Point
        $response = explode(
            ' ',
            str_replace(
                [
                    "GeomFromText('",
                    "'",
                    'POINT(',
                    ')'
                ],
                '',
                $value
            )
        );
        return new Point($response[0], $response[1]);
    }

    public function newQuery($excludeDeleted = true)
    {
        $raw = ' astext(position) as position ';
        return parent::newQuery($excludeDeleted)->addSelect('*', \DB::raw($raw));
    }
}

1st December, 2017

wildwestriverrider started a new conversation 5.5 Point Type Column • 9 months ago

How do you insert/update data for the new point type column in Laravel 5.5?

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.