From 801b129c51cf50353bd86ed1481365b018cefa43 Mon Sep 17 00:00:00 2001 From: Romain 'Maz' BILLOIR Date: Tue, 6 Dec 2022 12:22:45 +0100 Subject: [PATCH] Support upsert --- src/Eloquent/Builder.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index 3c6f1b1..3f74ffe 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -21,6 +21,24 @@ public function update(array $values) return parent::update($values); } + public function upsert(array $values, $uniqueBy, $update = null) + { + foreach ($values as &$row) { + foreach ($row as $column => &$value) { + if ($value instanceof GeometryInterface) { + if (is_null($this->model)) { + $value = $this->asWKT($value); + } else { + $attrs = $this->model->getPostgisType($column); + $value = $this->model->asWKT($value, $attrs); + } + } + } + } + + return parent::upsert($values, $uniqueBy, $update); + } + protected function asWKT(GeometryInterface $geometry) { return $this->getQuery()->raw(sprintf("%s.ST_GeogFromText('%s')",