Skip to content

Commit

Permalink
[10.x] Revival of the reverted changes in 10.25.0: firstOrCreate `u…
Browse files Browse the repository at this point in the history
…pdateOrCreate` improvement through `createOrFirst` + additional query tests (laravel#48637)

* Revert "[10.x] Revert from using `createOrFirst` in other `*OrCreate` methods (laravel#48531)"

This reverts commit 408a3e3.

* test: 💍 Add `Builder::createOrFirst()` snapshot tests

* test: 💍 Add `Builder::firstOrCreate()` snapshot tests

* test: 💍 Add `Builder::updateOrCreate()` snapshot tests

* test: 💍 Add test stubs for `DatabaseEloquentHasManyTest`

* test: 💍 Add `HasMany::createOrFirst()` snapshot tests

* test: 💍 Add `HasMany::firstOrCreate()` snapshot tests

* test: 💍 Add `HasMany::updateOrCreate()` snapshot tests

* test: 💍 prepare HasManyThrough test

* test: 💍 Add HasManyThrough::CreateOrFirst() snapshot tests

* test: 💍 Add HasManyThrough::firstOrCreate() snapshot test

* test: 💍 Add HasManyThrough::updateOrCreate() snapshot test

* refactor: 💡 use createOrFirst in firstOrCreate

* test: 💍 fix test

* style: 💄 Apply StyleCI fixes

* docs: ✏️ Add missing FIXME comments

* refactor: 💡 Omit verbose arguments

* test: 💍 Rename `DatabaseEloquentHasManyThroughTest` with fixes

* test: 💍 Add `BelongsToMany::createOrFirst/firstOrCreate` tests

* test: 💍 Extract `DatabaseEloquentHasManyTest` cases with fixes

* test: 💍 Extract `DatabaseEloquentBuilderTest` cases with fixes

* test: 💍 refactoring

* test: 💍 Add `BelongsToMany::updateOrCreate` snapshot tests

---------

Co-authored-by: fuwasegu <admin@fuwasegu.com>
  • Loading branch information
2 people authored and timacdonald committed Oct 24, 2023
1 parent 107023c commit 12e07c0
Show file tree
Hide file tree
Showing 10 changed files with 1,670 additions and 33 deletions.
8 changes: 5 additions & 3 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ public function firstOrCreate(array $attributes = [], array $values = [])
return $instance;
}

return $this->create(array_merge($attributes, $values));
return $this->createOrFirst($attributes, $values);
}

/**
Expand Down Expand Up @@ -595,8 +595,10 @@ public function createOrFirst(array $attributes = [], array $values = [])
*/
public function updateOrCreate(array $attributes, array $values = [])
{
return tap($this->firstOrNew($attributes), function ($instance) use ($values) {
$instance->fill($values)->save();
return tap($this->firstOrCreate($attributes, $values), function ($instance) use ($values) {
if (! $instance->wasRecentlyCreated) {
$instance->fill($values)->save();
}
});
}

Expand Down
20 changes: 7 additions & 13 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ public function firstOrCreate(array $attributes = [], array $values = [], array
{
if (is_null($instance = (clone $this)->where($attributes)->first())) {
if (is_null($instance = $this->related->where($attributes)->first())) {
$instance = $this->create(array_merge($attributes, $values), $joining, $touch);
$instance = $this->createOrFirst($attributes, $values, $joining, $touch);
} else {
try {
$this->getQuery()->withSavepointIfNeeded(fn () => $this->attach($instance, $joining, $touch));
Expand Down Expand Up @@ -672,19 +672,13 @@ public function createOrFirst(array $attributes = [], array $values = [], array
*/
public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true)
{
if (is_null($instance = (clone $this)->where($attributes)->first())) {
if (is_null($instance = $this->related->where($attributes)->first())) {
return $this->create(array_merge($attributes, $values), $joining, $touch);
} else {
$this->attach($instance, $joining, $touch);
}
}

$instance->fill($values);
return tap($this->firstOrCreate($attributes, $values, $joining, $touch), function ($instance) use ($values) {
if (! $instance->wasRecentlyCreated) {
$instance->fill($values);

$instance->save(['touch' => false]);

return $instance;
$instance->save(['touch' => false]);
}
});
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public function firstOrCreate(array $attributes = [], array $values = [])
return $instance;
}

return $this->create(array_merge($attributes, $values));
return $this->createOrFirst(array_merge($attributes, $values));
}

/**
Expand Down Expand Up @@ -305,11 +305,11 @@ public function createOrFirst(array $attributes = [], array $values = [])
*/
public function updateOrCreate(array $attributes, array $values = [])
{
$instance = $this->firstOrNew($attributes);

$instance->fill($values)->save();

return $instance;
return tap($this->firstOrCreate($attributes, $values), function ($instance) use ($values) {
if (! $instance->wasRecentlyCreated) {
$instance->fill($values)->save();
}
});
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function firstOrNew(array $attributes = [], array $values = [])
public function firstOrCreate(array $attributes = [], array $values = [])
{
if (is_null($instance = $this->where($attributes)->first())) {
$instance = $this->create(array_merge($attributes, $values));
$instance = $this->createOrFirst($attributes, $values);
}

return $instance;
Expand Down Expand Up @@ -267,10 +267,10 @@ public function createOrFirst(array $attributes = [], array $values = [])
*/
public function updateOrCreate(array $attributes, array $values = [])
{
return tap($this->firstOrNew($attributes), function ($instance) use ($values) {
$instance->fill($values);

$instance->save();
return tap($this->firstOrCreate($attributes, $values), function ($instance) use ($values) {
if (! $instance->wasRecentlyCreated) {
$instance->fill($values)->save();
}
});
}

Expand Down
Loading

0 comments on commit 12e07c0

Please sign in to comment.