Skip to content

Commit

Permalink
fix updating nullable dates
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid committed Jun 19, 2017
1 parent 3a20ea2 commit 737bd83
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,10 @@ protected function isStandardDateFormat($value)
*/
public function fromDateTime($value)
{
return $this->asDateTime($value)->format(
$this->getDateFormat()
);
return $value == null
? $value : $this->asDateTime($value)->format(
$this->getDateFormat()
);
}

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

use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Model;

/**
* @group integration
*/
class EloquentModelTest extends TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');

$app['config']->set('database.default', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

public function setUp()
{
parent::setUp();

Schema::create('users', function ($table) {
$table->increments('id');
$table->timestamp('nullable_date')->nullable();
});
}

public function test_user_can_update_nullable_date()
{
$user = EloquentModelTestModel::create([
'nullable_date' => null,
]);

$user->fill([
'nullable_date' => $now = \Carbon\Carbon::now(),
]);
$this->assertTrue($user->isDirty('nullable_date'));

$user->save();
$this->assertEquals($now->toDateString(), $user->nullable_date->toDateString());
}
}

class EloquentModelTestModel extends Model
{
public $table = 'users';
public $timestamps = false;
protected $guarded = ['id'];
protected $dates = ['nullable_date'];
}

0 comments on commit 737bd83

Please sign in to comment.