PHPackages                             sietse85/nova-button - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sietse85/nova-button

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sietse85/nova-button
====================

(Nova 4+) A Laravel Nova package for adding buttons to your resources.

1.0.28(1y ago)37347.3k—8.3%17MITPHPPHP ^7.4|^8.0

Since May 20Pushed 1y ago1 watchersCompare

[ Source](https://github.com/sietse85/nova-button)[ Packagist](https://packagist.org/packages/sietse85/nova-button)[ RSS](/packages/sietse85-nova-button/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (29)Used By (0)

Laravel Nova 4+ Button
======================

[](#laravel-nova-4-button)

A Nova package for rendering buttons on index, detail and lens views.

Use buttons to trigger backend events, navigate Nova routes or visit links.

[![example-users](https://user-images.githubusercontent.com/57711725/152637226-e7047831-b726-4940-95c9-617db4d42de4.png)](https://user-images.githubusercontent.com/57711725/152637226-e7047831-b726-4940-95c9-617db4d42de4.png)

> **This package is a continuation of [dillingham/nova-button](https://github.com/dillingham/nova-button).**
>
> Created to be compatible with Nova 4.0+

Requirements
------------

[](#requirements)

WhatMinimumPHP&gt;=7.4Laravel&gt;=8.0Nova&gt;=4.0Installation
------------

[](#installation)

You can install this package by running the following command:

```
composer require sietse85/nova-button
```

To publish the config file, run the following command:

```
php artisan vendor:publish --tag="nova-button-config"
```

Usage
-----

[](#usage)

```
use Sietse85\NovaButton\Button;
```

```
public function fields(Request $request)
{
    return [
        ID::make('ID', 'id')->sortable(),
        Text::make('Name', 'name'),
        Button::make('Notify'),
    ];
}
```

### Quick links

[](#quick-links)

- [Confirm](#confirm)
- [Reload](#reload)
- [Action](#action)
- [Events](#events)
- [Texts](#texts)
- [State](#state)
- [Feedback](#feedback)
- [Nova routes](#nova-routes)
- [Links](#links)
- [Classes](#classes)
- [Styles](#styles)
- [Examples](#examples)

---

### Confirm

[](#confirm)

You can require a confirmation for destructive actions:

```
Button::make('Cancel Account')->confirm('Are you sure?'),
Button::make('Cancel Account')->confirm('Confirmation', 'Are you sure you want to cancel your account?'),
Button::make('Cancel Account')->confirm('Confirmation', 'Are you sure you want to cancel your account?', 'Cancel'),
```

### Reload

[](#reload)

You can reload the page after all events have finished.

```
Button::make('Notify')->reload()
```

If you click multiple buttons, reloading will wait for all buttons to finish.

If an error occurs, the page will not be reloaded.

### Action

[](#action)

You can trigger an action now. No ActionFields can be used. Will trigger for current resource only. Handle will be called immediately.

```
Button::make('Send mail')->action(SendConfirmationMail::class)
```

If for some reason the Model for the action is not resolved correctly you can use `modelForAction` to correct it.

### Events

[](#events)

By default, clicking the button will trigger an event via AJAX.

Default event: `Sietse85\NovaButton\Events\ButtonClick`.

The event will receive the resource model it was triggered from and the key:

- `$event->resource` = `model`
- `$event->key` = `"notify"`

You can override the key:

```
Button::make('Notify', 'notify-some-user')
```

And also the event:

```
Button::make('Notify')->event(App\Events\NotifyRequested::class)
```

You can listen to the event by creating a listener and registering it in your `EventServiceProvider`.

### Texts

[](#texts)

#### Title

[](#title)

You can set the title attribute for the button:

```
Button::make('Notify')->title('Button title')
```

#### Label

[](#label)

You can set the label for the button, which is shown on the detail, create and update views:

```
Button::make('Notify')->label('Button label')
```

#### Index name

[](#index-name)

You can set the index name for the button, which is shown on the index view as the table header:

```
Button::make('Notify')->indexName('Actions')
```

Default is set to the button name. You can also pass `null` to have no index name.

### State

[](#state)

#### Visibility

[](#visibility)

You can conditionally show the button:

```
Button::make('Activate')->visible($this->is_active === false),
Button::make('Deactivate')->visible($this->is_active === true),
```

Or, if you only want specific users to see the button:

```
Button::make('Notify')->visible($request->user()->can('notifyUser', $this))
```

Of course you can also use Nova's builtin methods, like for [authorization](https://nova.laravel.com/docs/3.0/resources/authorization.html#fields)or to limit visibility to [specific views](https://nova.laravel.com/docs/3.0/resources/fields.html#showing-hiding-fields).

If you want to show a button on the create or update views you can simply use Nova's builtin methods:

```
Button::make('Notify')->showOnCreating()->showOnUpdating()
```

#### Disabled

[](#disabled)

You can disable the button:

```
Button::make('Notify')->disabled()
Button::make('Notify')->disabled($this->is_complete === false)
```

### Feedback

[](#feedback)

When using events, you might want to provide visual feedback to the end user. This is especially useful for long running listeners.

```
Button::make('Notify')
    ->loadingText('Sending...')
    ->successText('Sent!')
    ->errorText('Something went wrong...')
```

There are 3 events and for each event you can provide the text and style:

EventTextStyle`loading``->loadingText('Sending...')``->loadingStyle('grey-outline')``success``->successText('Done!')``->successStyle('success')``error``->errorText('Something went wrong...')``->errorStyle('danger')`The defaults are defined in the [config file](https://github.com/sietse85/nova-button/blob/main/config/nova-button.php).

### Nova routes

[](#nova-routes)

You can also choose to navigate to any Nova route:

```
Button::make('Text')->index(App\Nova\User::class)
Button::make('Text')->detail(App\Nova\User::class, $this->user_id)
Button::make('Text')->create(App\Nova\User::class)
Button::make('Text')->edit(App\Nova\User::class, $this->user_id)
Button::make('Text')->lens(App\Nova\User::class, 'users-without-confirmation')
```

It's also possible to use a resource's filters:

```
Button::make('Text')
    ->index(App\Nova\Order::class)
    ->withFilters([
        App\Nova\Filters\UserOrders::class => $this->user_id,
        App\Nova\Filters\OrderStatus::class => 'active',
    ])
```

### Links

[](#links)

You can configure the button to open external links:

```
Button::make('Text')->link('https://nova.laravel.com')
Button::make('Text')->link('https://nova.laravel.com', '_self')
```

### Classes

[](#classes)

The button uses the following classes that you can style to your liking:

```
.nova-button
.nova-button-{resource-name}
.nova-button-success
.nova-button-error
.nova-button-loading
```

You can also add more classes to a button:

```
// One class
Button::make('Notify')->classes(['some-class'])

// Or multiple classes
Button::make('Notify')->classes(['some-class', 'another-class'])
```

You can also add more classes to the confirm-modal inner-div:

```
// One class
Button::make('Notify')->confirm('sure?')->modalClasses(['some-class'])

// Or multiple classes
Button::make('Notify')->confirm('sure?')->modalClasses(['some-class', 'another-class'])
```

### Styles

[](#styles)

This package uses [tailwind-css](https://tailwindcss.com) classes. The default class used is the `link` class.

You can define the class the button should use:

```
Button::make('Delete')->style('danger')
```

The default available classes are as follows:

FillOutlineLinkprimaryprimary-outlineprimary-linksuccesssuccess-outlinesuccess-linkwarningwarning-outlinewarning-linkdangerdanger-outlinedanger-linkinfoinfo-outlineinfo-linkgreygrey-outlinegrey-linkThe passed key refers to one of the classes defined in the [config file](https://github.com/sietse85/nova-button/blob/main/config/nova-button.php). You are free to change these classes or add your own.

Examples
--------

[](#examples)

### Lenses

[](#lenses)

You can use a button with [lenses](https://nova.laravel.com/docs/3.0/lenses/defining-lenses.html).

[![lens-example](https://user-images.githubusercontent.com/57711725/152637243-ebd753c2-5eda-4749-b8ba-c98ceb162e5b.png)](https://user-images.githubusercontent.com/57711725/152637243-ebd753c2-5eda-4749-b8ba-c98ceb162e5b.png)

First set up the lens:

```
