Skip to content

castAttribute does not handle nullable immutable date times #2729

Closed
@TimothyBJacobs

Description

@TimothyBJacobs
  • Laravel-mongodb Version: 4.1.1
  • PHP Version: 8.1
  • Database Driver & Version: 6.0.13

Description:

The castAttribute method is overloaded in the base Model class. Unlike in the Core Laravel hasAttributes trait, it is missing handling for null values. See this code snippet from Laravel:

if (is_null($value) && in_array($castType, static::$primitiveCastTypes)) {
    return $value;
}

Steps to reproduce

class MyModel extends \MongoDB\Laravel\Eloquent\Model {
    protected $casts = [
        'my_date' => 'immutable_datetime',
    ];
}

$model = new MyModel();
$model->my_date; // Should be null, instead is current time.

Expected behaviour

null should be returned.

Actual behaviour

The current time is returned.

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