Skip to content
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
22 changes: 19 additions & 3 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2513,7 +2513,7 @@ public function attributesToArray()
// Next we will handle any casts that have been setup for this model and cast
// the values to their appropriate type. If the attribute has a mutator we
// will not perform the cast on those attributes to avoid any confusion.
foreach ($this->casts as $key => $value) {
foreach ($this->getCasts() as $key => $value) {
if (! array_key_exists($key, $attributes) ||
in_array($key, $mutatedAttributes)) {
continue;
Expand Down Expand Up @@ -2785,7 +2785,23 @@ protected function mutateAttributeForArray($key, $value)
*/
protected function hasCast($key)
{
return array_key_exists($key, $this->casts);
return array_key_exists($key, $this->getCasts());
}

/**
* Get the casts array.
*
* @return array
*/
protected function getCasts()
{
if ($this->incrementing) {
return array_merge([
$this->getKeyName() => 'int',
], $this->casts);
}

return $this->casts;
}

/**
Expand Down Expand Up @@ -2820,7 +2836,7 @@ protected function isJsonCastable($key)
*/
protected function getCastType($key)
{
return trim(strtolower($this->casts[$key]));
return trim(strtolower($this->getCasts()[$key]));
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,22 @@ public function testToArrayIncludesCustomFormattedTimestamps()
$this->assertEquals('05-12-12', $array['updated_at']);
}

public function testIncrementingPrimaryKeysAreCastToIntegersByDefault()
{
EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);

$user = EloquentTestUser::first();
$this->assertInternalType('int', $user->id);
}

public function testDefaultIncrementingPrimaryKeyIntegerCastCanBeOverwritten()
{
EloquentTestUserWithStringCastId::create(['email' => 'taylorotwell@gmail.com']);

$user = EloquentTestUserWithStringCastId::first();
$this->assertInternalType('string', $user->id);
}

/**
* Helpers...
*/
Expand Down Expand Up @@ -672,3 +688,10 @@ public function imageable()
return $this->morphTo();
}
}

class EloquentTestUserWithStringCastId extends EloquentTestUser
{
protected $casts = [
'id' => 'string',
];
}
9 changes: 8 additions & 1 deletion tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ public function testTheMutatorCacheIsPopulated()

public function testRouteKeyIsPrimaryKey()
{
$model = new EloquentModelStub;
$model = new EloquentModelNonIncrementingStub;
$model->id = 'foo';
$this->assertEquals('foo', $model->getRouteKey());
}
Expand Down Expand Up @@ -1553,3 +1553,10 @@ public function getVisible()
return ['name', 'id'];
}
}

class EloquentModelNonIncrementingStub extends Illuminate\Database\Eloquent\Model
{
protected $table = 'stub';
protected $guarded = [];
public $incrementing = false;
}