Download the PHP package inspheric/nova-defaultable without Composer

On this page you can find all versions of the php package inspheric/nova-defaultable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package nova-defaultable

Laravel Nova Defaultable Fields

Populate default values for Nova fields when creating resources and now supports resource actions (since v1.2)!

Latest Version on Packagist Total Downloads

Installation

Install the package into a Laravel app that uses Nova with Composer:

Note: This package is currently incompatible with Nova version 3.4.0 and above.

(Optional) If you want to use the defaultLast() method when creating resources (see below), you need to add the trait Inspheric\NovaDefaultable\HasDefaultableFields to your base Resource class (located at app\Nova\Resource.php):

(Optional) If you want to use the defaultLast() method on resource actions, you need to add the trait Inspheric\NovaDefaultable\HasDefaultableActionFields to every Action class (located at app\Nova\Actions\, one by one) that you wish to use defaultable fields on:

Basic Usage

When creating resources or running resource actions, there may be values which can be defaulted to save the user time, rather than needing to be entered into a blank form every time. This could include populating 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.

This package plugs into existing fields and provides two simple methods to supply a default value.

Note: The defaultable behaviour below 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; however, the last used value will be stored on any successful save request, and will be defaulted on a later 'create'/'attach' request.

Default any value

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

or:

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

Special cases

Default a BelongsTo field

To use the default() method on a Nova BelongsTo field, you can supply either:

Default a MorphTo field

To use the default() method on a Nova MorphTo field, you can supply either:

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:

This will be useful in the following scenarios:

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 (see Configuration).

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

Note: The defaultLast() method handles the morph type for MorphTo fields automatically.

Note: Because the "Select Action" dropdown is not refreshed after an action is run on the index view, defaultLast() cannot repopulate each last value if you run the action several times while on the same index view. If you need the value to be repopulated every time on the index view, you can set the property $refreshIndex = true on the action class, e.g.

When the action is run from the index view, it will return a redirect response to refresh the whole page. It has no effect if the action is run from the detail view, because Nova already refreshes the page after each action automatically.

:confounded: I don't really like this workaround but can't think of an alternative. I would be happy to hear other ideas.

Note: If you set $refreshIndex = true, and you return your own action response from the action's handle() method, it will be ignored on the index view because it is overridden by the redirect response. It will behave as normal on the detail view.

Display using a callback

Both the default($value, callable $callback = null) and defaultLast(callable $callback = null) methods can take a callback as the final parameter which will transform the defaulted value (whether retrieved from cache or passed to the default() method) before it is populated, e.g.

This can be used, for example, to increment a value each time a new resource is created. Note: This is a convenience only and should not be relied upon for uniqueness or strictly sequential incrementing.

Default last value or static value

If the user does not yet have a 'last' value stored, or the cache has expired, the value for defaultLast() will be blank. If you want to fall back to another value if nothing is found in the cache, you can simply do this in the callback, e.g.

In this example, the author of the first record created will default to the current user, but every subsequent record will default to the last value saved.

Advanced Usage

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. There is specific behaviour for the BelongsTo and MorphTo fields, as described above.

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.

The DefaultableField::extend() method takes the class name of your custom field and a callback which receives $field (the resolved Field instance) and $value (the value that was passed to default() or retrieved from cache by defaultLast()).

You must return the $field from your callback, and it is suggested that you use $field->withMeta() to send the appropriate metadata that will cause the field to be prepopulated, e.g. in your App\Providers\NovaServiceProvider:

Note: If you are using extend() as above, the basic defaulting functionality of this package is completely overridden, so you must ensure that your own callback sets the field's value correctly. This is usually done by setting the 'value' meta key, but your field may differ, or may need additional meta keys to be set (such as 'belongsToId' in the case of a BelongsTo field).

You can pass an array of field types as the first argument to use the same callback on all of them, i.e.

Alternatively, Inspheric\NovaDefault\DefaultableField is macroable, so you can add a macro and then use the macro's name as a string as the second argument for the extend() method, i.e.

Configuration

The configuration can be published using:

The configuration file contains two keys:


All versions of nova-defaultable with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package inspheric/nova-defaultable contains the following files

Loading the files please wait ....