Model Object
toggle()
Signature
Section titled “Signature”toggle() — returns boolean
Available in: model
Category: CRUD Functions
Description
Section titled “Description”Assigns to the property specified the opposite of the property’s current boolean value.
Throws an error if the property cannot be converted to a boolean value.
Returns this object if save called internally is false.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
property | string | yes | — | |
save | boolean | no | true | Argument to decide whether save the property after it has been toggled. |
Examples
Section titled “Examples”// 1. Toggle a boolean property and save it immediately (default behavior)
user = model("User").findByKey(58);
// Returns true if the record was saved successfully, false otherwise
isSuccess = user.toggle("isActive");
// 2. Toggle a boolean property without saving to the database
user = model("User").findByKey(58);
user.toggle(property="isActive", save=false);
// user.isActive is now flipped in memory; call user.save() later to persist
// 3. Use the dynamic toggle helper generated by Wheels
user = model("User").findByKey(58);
isSuccess = user.toggleIsActive();