Skip to content

Commit

Permalink
Ensure createOrFirst does not return null
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Aug 28, 2023
1 parent 4c5d4d5 commit 43e7694
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public function createOrFirst(array $attributes = [], array $values = [])
try {
return $this->withSavepointIfNeeded(fn () => $this->create(array_merge($attributes, $values)));
} catch (UniqueConstraintViolationException) {
return $this->useWritePdo()->where($attributes)->first();
return $this->useWritePdo()->where($attributes)->firstOrFail();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,11 @@ public function createOrFirst(array $attributes = [], array $values = [], array
}

try {
return tap($this->related->where($attributes)->first(), function ($instance) use ($joining, $touch) {
return tap($this->related->where($attributes)->firstOrFail(), function ($instance) use ($joining, $touch) {
$this->getQuery()->withSavepointIfNeeded(fn () => $this->attach($instance, $joining, $touch));
});
} catch (UniqueConstraintViolationException) {
return (clone $this)->useWritePdo()->where($attributes)->first();
return (clone $this)->useWritePdo()->where($attributes)->firstOrFail();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function createOrFirst(array $attributes = [], array $values = [])
try {
return $this->getQuery()->withSavepointIfNeeded(fn () => $this->create(array_merge($attributes, $values)));
} catch (UniqueConstraintViolationException) {
return $this->useWritePdo()->where($attributes)->first();
return $this->useWritePdo()->where($attributes)->firstOrFail();
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function testCreateOrFirstMethodWithValuesFindsFirstModel()
});
$relation->getQuery()->shouldReceive('useWritePdo')->once()->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(stdClass::class));
$relation->getQuery()->shouldReceive('firstOrFail')->once()->with()->andReturn($model = m::mock(stdClass::class));

$this->assertInstanceOf(stdClass::class, $found = $relation->createOrFirst(['foo' => 'bar'], ['baz' => 'qux']));
$this->assertSame($model, $found);
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseEloquentMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public function testCreateOrFirstMethodFindsFirstModel()
});
$relation->getQuery()->shouldReceive('useWritePdo')->once()->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class));
$relation->getQuery()->shouldReceive('firstOrFail')->once()->with()->andReturn($model = m::mock(Model::class));

$this->assertInstanceOf(Model::class, $relation->createOrFirst(['foo']));
}
Expand All @@ -255,7 +255,7 @@ public function testCreateOrFirstMethodWithValuesFindsFirstModel()
});
$relation->getQuery()->shouldReceive('useWritePdo')->once()->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class));
$relation->getQuery()->shouldReceive('firstOrFail')->once()->with()->andReturn($model = m::mock(Model::class));

$this->assertInstanceOf(Model::class, $relation->createOrFirst(['foo' => 'bar'], ['baz' => 'qux']));
}
Expand Down

0 comments on commit 43e7694

Please sign in to comment.