Skip to content

Eloquent isDirty() & getDirty() doesn't work as intended with tiny int (AKA boolean) #16823

@arcanedev-maroc

Description

@arcanedev-maroc
  • Laravel Version: 5.3.28
  • PHP Version: 7.1.0
  • Database Driver & Version: SQLite 3

Description:

I've a table with boolean column is_robot generated with a migration as $table->boolean('is_robot')->default(false);.

And in Database GUI, is declared as integer (SQLite connection).

The problem is when i want update an existed record with eloquent model (i use the $casts to convert is_robot as boolean):

Session {#630 ▼
  ...
  #attributes: array:13 [▼
    "id" => "4"
    ...
    "is_robot" => false
    ...
  ]
  #original: array:13 [▼
    "id" => "4"
    ...
    "is_robot" => "0"
    ...
  ]
  ...
}

This is after i fill() the is_robot attribute with false (it was "0" on retrieve).

Steps To Reproduce:

So when i use fill method and check if the model was changed with isDirty():

$session->fill([
    'is_robot' => false,
]);

if ($session->isDirty()) {
    $session->save();
    // Other stuff...
}

The isDirty() method always return true when is_robot in the $original array equals to "0" (string zero) and in the $attributes array equals to false.

I think the getDirty() doesn't check if the attribute has a cast.

I didn't test this issue with a mysql database (for the time being)

Best regards,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions