marshmallow / button-field
A Laravel Nova field.
Installs: 1 205
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Language:Vue
Requires
- php: ^7.3|^8.0
README
Nova Button Package
Installation
You can install the package via composer:
composer require marshmallow/button-field
Usage
By default this package will resolve the link from the column you provide when calling the make method. By calling the resolveUsing
method you can return your own generated link to the button.
use Marshmallow\ButtonField\ButtonField; ButtonField::make('Certificate')->resolveUsing(function () { return '___YOUR_LINK_GOES_HERE___'; }),
Methods
button()
When you call the button
method, this will return a default Laravel Nova Button. This should have the same styling as the Update and Update & Continue Editing buttons in your Nova installation. This method is called by default so this behaviour should be available out of the box.
$button_field->button();
target()
You can call the target
method to change the behaviour of your button.
$button_field->target('_blank'); // DEFAULT $button_field->target('_self');
download()
When you call the download
method, the styling of the button will change. It will be a smaller button with a download icon next to it. We will also add the “download” tag to the button so the browser knows you want to download something. You can see an example of this in the screenshot at the top of this page.
$button_field->download();
setButtonText()
The default text of the created button is “Download”. You can change this by calling the setButtonText
method.
$button_field->setButtonText('Go to user profile');
visibleWhen()
You can use the visibleWhen
method if a button should only be visible when a condition applies.
$button_field->visibleWhen(function() { // Call methods on your resource return $this->certificateIsAvailable(); }, __('Certificate is not generated yet'));
onClick()
You can use the onClick
method to run any kind of action to your model. This is a very powerfull method that allows you to do anything! This currently only works on the text type button so we must set it to text()
. Please check the example below how to implement this on your Nova Resource.
ButtonField::make(__('Send invoice')) ->text() ->setButtonText(__('Send invoice')) ->onClick(SendInvoice::class),
Now we need to create an action class that will be run when the button is clicked. You can create this anywhere you like. This could be the location app/Actions/Nova/SendInvoice.php. In this file you create a class that implements the OnClickInterface interface. You need to create 3 methods in this class. Please check the example below.
<?php namespace App\Actions\Nova; use Exception; use Illuminate\Database\Eloquent\Model; use Marshmallow\ButtonField\Contracts\OnClickInterface; class SendInvoice implements OnClickInterface { public function execute(Model $model): void { // Run your code! } public function success(): string { return __('Invoice is end successfully'); } public function error(Exception $exception): string { return __('Something went wrong: :exception', [ 'exception' => $exception->getMessage(), ]); } }
Run Nova Actions
You can now run your Nova Actions using the button field. This means its now possible to run a nova action from the form view as well. Make sure the action is registered in your resources actions method and then use the example code below.
ButtonField::make(__('Nova Action')) ->action( action: new \App\Nova\Actions\DemoAction, resourceId: $this->resource->id, label: 'Run the demo dude!' ),
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
composer test
Security
If you discover any security related issues, please email stef@marshmallow.dev instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.