Skip to content

Commit 7740c10

Browse files
committed
Fix ID handling when using insert method instead of save
1 parent 1bb06ec commit 7740c10

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Query/Builder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,10 @@ public function insert(array $values)
745745
$values = [$values];
746746
}
747747

748-
$values = $this->aliasIdForQuery($values);
748+
$values = array_map(
749+
$this->aliasIdForQuery(...),
750+
$values,
751+
);
749752

750753
$options = $this->inheritConnectionOptions();
751754

tests/QueryBuilderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ public function testInsert()
126126
$this->assertIsArray($user->tags);
127127
}
128128

129+
#[TestWith([true])]
130+
#[TestWith([false])]
131+
public function testInsertWithCustomId(bool $renameEmbeddedIdField)
132+
{
133+
$connection = DB::connection('mongodb');
134+
$connection->setRenameEmbeddedIdField($renameEmbeddedIdField);
135+
136+
$data = ['id' => 'abcdef', 'name' => 'John Doe'];
137+
138+
DB::table('users')->insert($data);
139+
140+
$user = User::find('abcdef');
141+
$this->assertInstanceOf(User::class, $user);
142+
$this->assertSame('abcdef', $user->id);
143+
}
144+
129145
public function testInsertGetId()
130146
{
131147
$id = DB::table('users')->insertGetId(['name' => 'John Doe']);

0 commit comments

Comments
 (0)