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

[9.x] Fix inconsistent results of firstOrNew() when using withCasts() #41257

Merged
Merged
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
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public function newInstance($attributes = [], $exists = false)
// This method just provides a convenient way for us to generate fresh model
// instances of this current model. It is particularly useful during the
// hydration of new objects via the Eloquent query builder instances.
$model = new static((array) $attributes);
$model = new static;

$model->exists = $exists;

Expand All @@ -505,6 +505,8 @@ public function newInstance($attributes = [], $exists = false)

$model->mergeCasts($this->casts);

$model->fill((array) $attributes);

return $model;
}

Expand Down
90 changes: 90 additions & 0 deletions tests/Database/DatabaseEloquentWithCastsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Illuminate\Tests\Database;

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
use PHPUnit\Framework\TestCase;

class DatabaseEloquentWithCastsTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

$db = new DB;

$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);

$db->bootEloquent();
$db->setAsGlobal();

$this->createSchema();
}

protected function createSchema()
{
$this->schema()->create('times', function ($table) {
$table->increments('id');
$table->time('time');
$table->timestamps();
});
}

public function testWithFirstOrNew()
{
$time1 = Time::query()->withCasts(['time' => 'string'])
->firstOrNew(['time' => '07:30']);

Time::query()->insert(['time' => '07:30']);

$time2 = Time::query()->withCasts(['time' => 'string'])
->firstOrNew(['time' => '07:30']);

$this->assertSame('07:30', $time1->time);
$this->assertSame($time1->time, $time2->time);
}

public function testWithFirstOrCreate()
{
$time1 = Time::query()->withCasts(['time' => 'string'])
->firstOrCreate(['time' => '07:30']);

$time2 = Time::query()->withCasts(['time' => 'string'])
->firstOrCreate(['time' => '07:30']);

$this->assertSame($time1->id, $time2->id);
}

/**
* Get a database connection instance.
*
* @return \Illuminate\Database\Connection
*/
protected function connection()
{
return Eloquent::getConnectionResolver()->connection();
}

/**
* Get a schema builder instance.
*
* @return \Illuminate\Database\Schema\Builder
*/
protected function schema()
{
return $this->connection()->getSchemaBuilder();
}
}

class Time extends Eloquent
{
protected $guarded = [];

protected $casts = [
'time' => 'datetime',
];
}