Skip to content

Commit ed949b9

Browse files
committed
PHPORM-171 Set timestamps when using createOrFirst
1 parent 8f7bf77 commit ed949b9

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Eloquent/Builder.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ public function createOrFirst(array $attributes = [], array $values = []): Model
204204
// Apply casting and default values to the attributes
205205
// In case of duplicate key between the attributes and the values, the values have priority
206206
$instance = $this->newModelInstance($values + $attributes);
207+
208+
/* @see \Illuminate\Database\Eloquent\Model::performInsert */
209+
if ($instance->usesTimestamps()) {
210+
$instance->updateTimestamps();
211+
}
212+
207213
$values = $instance->getAttributes();
208214
$attributes = array_intersect_key($attributes, $values);
209215

tests/ModelTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Carbon\Carbon;
88
use DateTime;
9+
use DateTimeInterface;
910
use Generator;
1011
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
1112
use Illuminate\Database\Eloquent\ModelNotFoundException;
@@ -1048,12 +1049,17 @@ public function testNumericFieldName(): void
10481049

10491050
public function testCreateOrFirst()
10501051
{
1052+
Carbon::setTestNow('2010-06-22');
1053+
$createdAt = Carbon::now()->getTimestamp();
10511054
$user1 = User::createOrFirst(['email' => '[email protected]']);
10521055

10531056
$this->assertSame('[email protected]', $user1->email);
10541057
$this->assertNull($user1->name);
10551058
$this->assertTrue($user1->wasRecentlyCreated);
1059+
$this->assertEquals($createdAt, $user1->created_at->getTimestamp());
1060+
$this->assertEquals($createdAt, $user1->updated_at->getTimestamp());
10561061

1062+
Carbon::setTestNow('2020-12-28');
10571063
$user2 = User::createOrFirst(
10581064
['email' => '[email protected]'],
10591065
['name' => 'John Doe', 'birthday' => new DateTime('1987-05-28')],
@@ -1064,6 +1070,8 @@ public function testCreateOrFirst()
10641070
$this->assertNull($user2->name);
10651071
$this->assertNull($user2->birthday);
10661072
$this->assertFalse($user2->wasRecentlyCreated);
1073+
$this->assertEquals($createdAt, $user1->created_at->getTimestamp());
1074+
$this->assertEquals($createdAt, $user1->updated_at->getTimestamp());
10671075

10681076
$user3 = User::createOrFirst(
10691077
['email' => '[email protected]'],
@@ -1075,6 +1083,8 @@ public function testCreateOrFirst()
10751083
$this->assertSame('Jane Doe', $user3->name);
10761084
$this->assertEquals(new DateTime('1987-05-28'), $user3->birthday);
10771085
$this->assertTrue($user3->wasRecentlyCreated);
1086+
$this->assertEquals($createdAt, $user1->created_at->getTimestamp());
1087+
$this->assertEquals($createdAt, $user1->updated_at->getTimestamp());
10781088

10791089
$user4 = User::createOrFirst(
10801090
['name' => 'Robert Doe'],

0 commit comments

Comments
 (0)