Skip to content

Commit 6836d79

Browse files
authored
Merge pull request #7 from trybeapp/insert-result
feat: store and retrieve result of insert
2 parents 525fdff + 625d018 commit 6836d79

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/Collection.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66

77
class Collection extends BaseCollection
88
{
9-
public function addToIndex()
9+
/**
10+
* @var array
11+
*/
12+
protected $addToIndexResult;
13+
14+
/**
15+
* @return bool
16+
*/
17+
public function addToIndex(): bool
1018
{
1119
if ($this->isEmpty()) {
12-
return;
20+
return true;
1321
}
1422

1523
$instance = $this->first();
@@ -25,8 +33,18 @@ public function addToIndex()
2533

2634
$success = $query->insert($docs->all());
2735

36+
$this->addToIndexResult = $query->getQuery()->getInsertResult();
37+
2838
unset($docs);
2939

3040
return $success;
3141
}
42+
43+
/**
44+
* @return array|null
45+
*/
46+
public function getAddToIndexResult(): ?array
47+
{
48+
return $this->addToIndexResult;
49+
}
3250
}

src/QueryBuilder.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class QueryBuilder extends BaseBuilder
3838

3939
protected $routing;
4040

41+
protected $insertResult;
42+
4143
/**
4244
* All of the supported clause operators.
4345
*
@@ -731,9 +733,9 @@ public function cursor()
731733
*/
732734
public function insert(array $values): bool
733735
{
734-
$result = $this->connection->insert($this->grammar->compileInsert($this, $values));
736+
$this->insertResult = $this->connection->insert($this->grammar->compileInsert($this, $values));
735737

736-
return empty($result['errors']);
738+
return empty($this->insertResult['errors']);
737739
}
738740

739741
/**
@@ -767,6 +769,11 @@ public function delete($id = null): bool
767769
return !empty($result['found']);
768770
}
769771

772+
/**
773+
* @param mixed $method
774+
* @param mixed $parameters
775+
* @return void
776+
*/
770777
public function __call($method, $parameters)
771778
{
772779
if (Str::startsWith($method, 'filterWhere')) {
@@ -776,6 +783,14 @@ public function __call($method, $parameters)
776783
return parent::__call($method, $parameters);
777784
}
778785

786+
/**
787+
* @return array
788+
*/
789+
public function getInsertResult(): array
790+
{
791+
return array_column($this->insertResult['items'], 'index');
792+
}
793+
779794
/**
780795
* @return bool
781796
*/

0 commit comments

Comments
 (0)