Skip to content

Model methods ​

The HasCustomFields trait adds these methods to your model.

Values ​

MethodReturnsNotes
listCustomFields()array<string, mixed>Every value keyed by field key, as stored (no casts), in field order
getCustomField($key, $default = null)mixedStored value, no casts. Throws for unknown keys in strict mode.
setCustomField($key, $value)staticValidate and write one value right away, without saving the model
syncCustomFields(array $values)staticValidate and write several values right away. Only the given keys change.
forgetCustomField($key)staticDelete one stored value
hasCustomField($key)boolTrue when a non-null value is stored. Never throws.

Definitions and rules ​

MethodReturnsNotes
customFieldDefinitions()Collection<string, CustomField>Visible field definitions, active or not, keyed by key
customFieldRules()arrayValidation rules for a form request
customFieldScope()?ModelThe tenant that this record's fields come from. Override it.
customFieldValues()MorphManyThe raw value rows

Query scopes ​

ScopeExample
withCustomFields()User::withCustomFields()->get()
withoutCustomFields()User::withoutCustomFields()->get()
whereCustomField($key, $operator, $value)->whereCustomField('age', '>=', 18)
whereCustomFieldIn($key, array $values)->whereCustomFieldIn('tier', ['a', 'b'])
whereCustomFieldContains($key, $value)->whereCustomFieldContains('tags', 'vip')
whereCustomFieldNull($key)->whereCustomFieldNull('notes')
whereCustomFieldNotNull($key)->whereCustomFieldNotNull('notes')

Properties ​

PropertyDefaultWorks like
$customFieldsevery field$fillable, for custom fields
$customFieldCastsdates → Carbon$casts
$loadCustomFieldsfalse$with = ['customFieldValues']

Overridden Eloquent methods ​

The trait overrides getAttribute(), setAttribute(), isFillable(), fillableFromArray(), attributesToArray() and save(). If your model overrides one of these too, call the trait's version. See Limitations.

Released under the MIT License.