Default values for Nova fields

When creating Nova resources or running resource actions, users are always presented with a blank form. All fields are empty and all checkboxes are unchecked.

Often there may be values which can be defaulted or prepopulated to save the user time, rather than needing to be reentered every time a new resource is created.

This could include setting the user_id on a resource that the current user owns, repeating the same ‘parent’ record for several new records in a row, starting with a checkbox in a checked state, or populating an incrementing value, e.g. an invoice number.

A default value is automatically populated and the checkbox is automatically checked on the create form.

The inspheric/nova-defaultable package plugs into existing Nova field classes and provides two simple methods to supply a default value.

The defaultable behaviour is only applicable on the resource’s ‘create’ or ‘attach’ form, or on action requests. Fields will not be defaulted on ‘update’ or ‘update-attached’ requests.

Default any value

Use the default($value) method on any standard Nova field to populate a simple value, e.g.

Text::make('Name')
->default('Default Name'),

or:

Boolean::make('Active')
->default(true),

The default() method can also take a callback as the first parameter, which will return the value to be populated:

Text::make('Name')
->default(function(NovaRequest $request) {
return $request->user()->name.'\'s New Resource';
}),

Default the last saved value

Use the defaultLast() method to cache the last value that was saved for this field and repopulate it the next time this field is resolved:

Text::make('Name')
->defaultLast(),

This will be useful in the following scenarios:

  • After saving (create or update) one resource, the last value will be repopulated when creating the next new resource.
  • After running an action on one resource, the last value will be repopulated when running the same action on another resource. *

The value is cached uniquely to the user, resource class, action name (if applicable), field, and attribute. The default cache duration is an hour, but this is customisable.

The last value for a field will be stored on any successful save (create or update) request, and will be defaulted only on the next ‘create’/’attach’ request.

This can be used, for example, to speed up creating multiple resources one after another with the same parent resource, e.g.

BelongsTo::make('Author')
->defaultLast(),

* There are some caveats to this behaviour explained in the readme.

Extend

Out of the box, the package supports all standard Nova fields which have a single value and can be edited on the ‘create’ form.

This package does not support any of the fields that implement Laravel\Nova\Contracts\ListableField, such as HasMany, BelongsToMany etc., or fields that extend Laravel\Nova\Fields\File, such as File, Image or Avatar.

Any custom field with a single value which extends Laravel\Nova\Fields\Field should work without customisation. However, if required, you can extend the behaviour to support custom field types which need additional metadata to be populated.