Troubles with Laravel Crypt::decrypt

I've been working with a load of sensitive data recently, so I've had to encrypt loads of database fields. This with Laravel, is pretty damn easy. Check out my previous post for my Support model that encrypts and decrypts on the fly.

For the past two hours, that being said, I've been pulling my hair out with the error "The payload is invalid". Pretty much, it turns out to a really simple fix. I am slightly embarrassed it took me so long to catch this problem. Must be the Monday blues...

Old Support.php class Support extends Model { protected $encrypted = [];

  public function getAttribute($key)
  {
      if (array_key_exists($key, array_flip($this->encrypted)))
      {  
          return Crypt::decrypt(parent::getAttribute($key));
      }

      return parent::getAttribute($key);
  }

public function setAttribute($key, $value) { if (array_key_exists($key, array_flip($this->encrypted))) { parent::setAttribute($key, Crypt::encrypt($value)); return; }

      parent::setAttribute($key, $value);
  }
}

Basically, it boils down to if Crypt::decrypt tries to decrypt a blank value, it'll throw the error "The payload is invalid" so add a simple check in and voilá!

if(parent::getAttribute($key) == '') {
    return '';
}