Skip to content

Fix cast issue #2651

Closed
Closed
@rbruhn

Description

@rbruhn

As reported in #2257 , #2199 , #1580 there is an issue with casting and saving to the database. @alcaeus asked for a test example in a new issue. A sample test was provided by @hans-thomas but I figured I would add one here that's more inline with current tests.
The pull request from #2257 works.

First, change User model to make age and integer. This should not effect any other tests as an integer is used in those tests.

protected $casts            = [
    'birthday' => 'datetime',
    'entry.date' => 'datetime',
    'member_status' => MemberStatus::class,
    'age' => 'int'
];

In ModelTest, add the following test. Here we set age as string. It will fail because the cast type is not being saved to the database.

public function testCasting(): void
{
    $user        = new User();
    $user->name  = 'John Doe';
    $user->title = 'admin';
    $user->age   = '35';

    $user->save();

    $this->assertTrue($user->exists);
    $this->assertEquals(1, User::count());

    $raw = $user->getAttributes();
    $this->assertIsInt($raw['age']);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions