Learn how to define lenses in Nova.
nova:lens
Artisan command. By default, Nova will place newly generated lenses in the app/Nova/Lenses
directory:
query
and fields
methods. The query
method is responsible for building the Eloquent query that is needed to retrieve the desired data, while the fields
method returns an array of fields that should be displayed when viewing the lens.
To learn more, let’s take a look at a complete lens definition that displays users and their lifetime revenue. As you can see in the example below, the query
method will take advantage of the withFilters
and withOrdering
methods offered by the LensRequest
in order to instruct Nova to also apply any selected filters and ordering constraints to the query:
query
method has full control of the Eloquent query used to retrieve the lens data. The fields
method may leverage any of Nova’s fields in order to appropriately display the data retrieved by the query.
columns
method has been extracted from the query
method for readability. It is not “required” and is not a “feature” of lenses.When writing your lens query, you should always try to include the resource’s ID as a selected column. If the ID is not included, Nova will not be able to display the “Select All Matching” option for the lens. In addition, the resource deletion menu will not be available.withOrdering
and withFilters
methods are used to apply orderings and filters to lens queries and should always be applied in the query
method. Both methods accept the $query
as the first parameter, and the withOrdering
method accepts a closure as its second parameter. The closure passed to the withOrdering
method should apply the default ordering to the query that will be applied if no other ordering has been selected from the Nova dashboard:
polling
property of your lens class:
pollingInterval
property on your lens class. The pollingInterval
defines the number of seconds Nova should wait before fetching new records:
showPollingToggle
property on your lens class as true
:
filters
method. This method allows you to attach any of your existing filters to the lens:
actions
method. This method allows you to attach any of your existing actions to the lens:
actions
method on the lens to define a custom set of actions that should be available to the lens.cards
method. This method allows you to attach any of your existing metrics to the lens: