lambooni
6 months ago

Should model casting force the value?

Posted 6 months ago by lambooni

I am using attribute casting on my model as follows:

    protected $casts = [
        'processed' => 'boolean',
    ];

I am getting data from an API which returns 1 and "" (empty string) for true and false. I would have expected after casting as the above, creating an entry results in a SQL error Incorrect integer value.

I therefore have to force the false values on each boolean field prior to creating, like:

           foreach($this->casts as $field => $cast){
                    if($cast === 'boolean'){
                        if(empty($data[$field])) $data[$field] = false;
                    }
                }

Is this expected behaviour when using attribute casting?

Please sign in or create an account to participate in this conversation.