richardh's avatar

Cast json true to boolean in the model

I have an json call that sends 'true' as a property and get the error

  "message": "SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'true' for column 'suspended'

I have tried to use the

 protected $casts = [
       'suspended' => 'boolean'
    ];

but this doesn't work either .

Does anybody know how this is achieved.

0 likes
2 replies
crnkovic's avatar
crnkovic
Best Answer
Level 43

Show us a code snippets, where you're using the attribute, migration, etc.

suspended column should be INT and casts 1 to true, 0 to false.

// migration
$table->boolean('suspended');

// model
protected $casts = ['suspended' => 'boolean'];

// usage
if ($model->suspended)

$model->suspended = false;
$model->suspended = true;

Please or to participate in this conversation.