Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Feat: Add firstOrInsert method to Query Builder with tests #54598

Draft
wants to merge 1 commit into
base: 12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3915,6 +3915,26 @@ public function updateOrInsert(array $attributes, array|callable $values = [])
return (bool) $this->limit(1)->update($values);
}

/**
* Get the first record matching the attributes or insert it.
*
* @param array $attributes
* @param array $values
* @return object
*/
public function firstOrInsert(array $attributes, array $values = []): object
{
$record = $this->where($attributes)->first();

if ($record !== null) {
return $record;
}

$this->insert(array_merge($attributes, $values));

return $this->where($attributes)->first();
}

/**
* Insert new records or update the existing ones.
*
Expand Down
66 changes: 66 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4141,6 +4141,72 @@ public function testUpdateOrInsertMethodWorksWithEmptyUpdateValues()
$builder->shouldNotHaveReceived('update');
}

public function testFirstOrInsertMethodRecordExists()
{
$builder = m::mock(Builder::class.'[where,first,insert]', [
m::mock(ConnectionInterface::class),
new Grammar,
m::mock(Processor::class),
]);

$existingRecord = new stdClass();
$existingRecord->name = 'Existing User';
$existingRecord->email = '[email protected]';

$builder->shouldReceive('where')
->once()
->with(['email' => '[email protected]'])
->andReturn(m::self());

$builder->shouldReceive('first')
->once()
->andReturn($existingRecord);

$builder->shouldNotReceive('insert');

$result = $builder->firstOrInsert(['email' => '[email protected]'], ['name' => 'New Name']);

$this->assertInstanceOf(stdClass::class, $result);
$this->assertEquals('Existing User', $result->name);
$this->assertEquals('[email protected]', $result->email);
}

public function testFirstOrInsertMethodRecordDoesNotExist()
{
$builder = m::mock(Builder::class.'[where,first,insert]', [
m::mock(ConnectionInterface::class),
new Grammar,
m::mock(Processor::class),
]);

$insertedRecord = new stdClass();
$insertedRecord->name = 'New User';
$insertedRecord->email = '[email protected]';

$builder->shouldReceive('where')
->twice()
->with(['email' => '[email protected]'])
->andReturn(m::self());

$builder->shouldReceive('first')
->once()
->andReturn(null);

$builder->shouldReceive('insert')
->once()
->with(['email' => '[email protected]', 'name' => 'New User'])
->andReturn(true);

$builder->shouldReceive('first')
->once()->andReturn($insertedRecord);

$result = $builder->firstOrInsert(['email' => '[email protected]'], ['name' => 'New User']);

$this->assertInstanceOf(stdClass::class, $result);
$this->assertEquals('New User', $result->name);
$this->assertEquals('[email protected]', $result->email);
}

public function testDeleteMethod()
{
$builder = $this->getBuilder();
Expand Down
Loading