Skip to content

Commit

Permalink
Support for Laravel 8.10.0
Browse files Browse the repository at this point in the history
Added support for upsert in Laravel 8.10.0
laravel/framework#34698
  • Loading branch information
sunaoka committed Oct 20, 2020
1 parent 38d0b10 commit 8386557
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 82 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -16,7 +16,7 @@ composer require sunaoka/laravel-postgres-extension
- [ ] tstzrange — Range of timestamp with time zone
- [ ] daterange — Range of date

- [x] UPSERT (ON CONFLICT DO UPDATE)
- [x] ~~UPSERT (ON CONFLICT DO UPDATE)~~

- [x] RETURNING
- UPDATE
Expand Down Expand Up @@ -80,7 +80,9 @@ echo $some->term->upper()->format('Y-m-d H:i:s'); // upper() or to()
// => 2020-10-01 23:59:59
```

### UPSERT (ON CONFLICT DO UPDATE)
### ~~UPSERT (ON CONFLICT DO UPDATE)~~

Laravel 8.10.0 adds support for upsert.

```php
$result = SomeModel::upsert([
Expand Down
17 changes: 0 additions & 17 deletions src/Eloquent/Builder.php
Expand Up @@ -9,23 +9,6 @@
*/
class Builder extends \Illuminate\Database\Eloquent\Builder
{
/**
* @param array $values
* @param array $columns
*
* @return bool|\Illuminate\Database\Eloquent\Collection
*/
public function upsert(array $values, array $columns)
{
$result = $this->toBase()->upsert($this->addUpdatedAtColumn($values), $columns);
if (empty($this->toBase()->returning)) {
return $result;
}

/** @var array $result */
return !empty($result) ? $this->hydrate($result) : null;
}

/**
* Update records in the database.
*
Expand Down
23 changes: 0 additions & 23 deletions src/Query/Builder.php
Expand Up @@ -71,27 +71,4 @@ public function delete($id = null)

return $this->connection->select($query, $bindings);
}

/**
* Upsert records in the database.
*
* @param array $values
* @param array $columns
*
* @return array|bool
*/
public function upsert(array $values, array $columns)
{
$query = $this->grammar->compileUpsert($this, $values, $columns);

$bindings = $this->cleanBindings(
$this->grammar->prepareBindingsForUpsert($this->bindings, $values)
);

if (empty($this->returning)) {
return $this->connection->insert($query, $bindings);
}

return $this->connection->select($query, $bindings);
}
}
40 changes: 0 additions & 40 deletions src/Query/Grammars/PostgresGrammar.php
Expand Up @@ -80,46 +80,6 @@ public function compileDelete(Builder $query)
return trim($sql);
}

/**
* Compile a upsert statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @param array $columns
* @return string
*/
public function compileUpsert(Builder $query, array $values, array $columns)
{
$values = $this->cleanColumns($values);

$conflict = collect($columns)->map(function ($value) {
return $this->wrap($value);
})->implode(', ');

$update = $this->compileUpdateColumns($query, $values);

$sql = $this->compileInsert($query, $values);
$sql .= " on conflict ({$conflict}) do update set {$update}";

$sql .= " {$this->compileReturning($query)}";

return trim($sql);
}

/**
* Prepare the bindings for an upsert statement.
*
* @param array $bindings
* @param array $values
* @return array
*/
public function prepareBindingsForUpsert(array $bindings, array $values): array
{
$bindings = $this->prepareBindingsForUpdate($bindings, $values);

return array_merge($bindings, $bindings);
}

/**
* Removes the table name from the column name.
*
Expand Down

0 comments on commit 8386557

Please sign in to comment.