Skip to content

Commit 34ace19

Browse files
implement fillAndInsertOrIgnore() and fillAndInsertGetId
1 parent f81111e commit 34ace19

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,28 @@ public function fillAndInsert(array $values)
384384
return $this->query->fillAndInsert($this->fillForInsert($values));
385385
}
386386

387+
/**
388+
* Insert (ignoring errors) into the database after merging the model's default attributes, setting timestamps, and casting values.
389+
*
390+
* @param array<int, array<string, mixed>> $values
391+
* @return int
392+
*/
393+
public function fillAndInsertOrIgnore(array $values)
394+
{
395+
return $this->query->fillAndInsertOrIgnore($this->fillForInsert($values));
396+
}
397+
398+
/**
399+
* Insert a record into the database and get its ID after merging the model's default attributes, setting timestamps, and casting values.
400+
*
401+
* @param array<string, mixed> $values
402+
* @return int
403+
*/
404+
public function fillAndInsertGetId(array $values)
405+
{
406+
return $this->query->fillAndInsertGetId($this->fillForInsert($values)[0]);
407+
}
408+
387409
protected function fillForInsert(array $values)
388410
{
389411
if (empty($values)) {

tests/Database/DatabaseEloquentIntegrationTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,6 +2718,45 @@ public function testCanFillAndInsertIntoHasManyRelationship()
27182718
$this->assertEquals(Carbon::parse('2025-02-24T15:16:55.000Z'), $user->posts->find(3)->published_at);
27192719
}
27202720

2721+
public function testFillAndInsertOrIgnoreIntoHasManyRelationship()
2722+
{
2723+
Str::createUuidsUsingSequence([
2724+
'00000000-0000-7000-0000-000000000000', // user
2725+
'11111111-1111-1111-1111-111111111111', // post id=1
2726+
'22222222-2222-2222-2222-222222222222', // post id=1 ignored
2727+
'33333333-3333-3333-3333-333333333333', // post id=3
2728+
]);
2729+
2730+
$user = tap(new UserWithUniqueStringIds(), function ($user) {
2731+
$user->forceFill(['name' => 'Taylor Otwell'])->save();
2732+
});
2733+
2734+
$attributes = [
2735+
['id' => 1, 'published_at' => now(), 'name' => 'ship of die'],
2736+
['id' => 3, 'published_at' => now(), 'name' => 'Welcome to the future of Laravel'],
2737+
];
2738+
2739+
$this->assertSame(1, $user->posts()->fillAndInsertOrIgnore(array_slice($attributes, 0, 1)));
2740+
$this->assertSame(1, $user->posts()->fillAndInsertOrIgnore(array_slice($attributes, 0, 2)));
2741+
2742+
$this->assertSame('11111111-1111-1111-1111-111111111111', $user->posts->find(1)->uuid);
2743+
$this->assertSame('33333333-3333-3333-3333-333333333333', $user->posts->find(3)->uuid);
2744+
}
2745+
2746+
public function testfillAndInsertGetIdIntoHasManyRelationship()
2747+
{
2748+
$user = tap(new UserWithUniqueStringIds(), function ($user) {
2749+
$user->forceFill(['name' => 'Taylor Otwell'])->save();
2750+
});
2751+
2752+
$id = $user->posts()->fillAndInsertGetId([
2753+
'name' => 'We must ship.',
2754+
'published_at' => now(),
2755+
]);
2756+
2757+
$this->assertSame(1, $id);
2758+
}
2759+
27212760
/**
27222761
* Helpers...
27232762
*/

0 commit comments

Comments
 (0)