GithubHelp home page GithubHelp logo

bsormagec / nova-dynamic-action-fields Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tunezilla/nova-dynamic-action-fields

1.0 1.0 0.0 30 KB

Dynamic Action Fields for Laravel Nova

Home Page: https://packagist.org/packages/tunezilla/dynamic-action-fields

License: MIT License

JavaScript 5.45% PHP 60.67% Vue 33.87%

nova-dynamic-action-fields's Introduction

Dynamic Action Fields for Laravel Nova

This package allows you to change the fields of a Laravel Nova Action depending on which resources are selected.

This is experimental, proceed at your own risk

Screenshot

Screenshot of Dynamic Action Fields

Installation

composer require tunezilla/dynamic-action-fields

Usage

  1. Add the DynamicFieldAction trait to your resource

  2. Change your public function fields() to public function fieldsForModels(Collection $models): array

Example:

<?php

namespace App\Nova\Actions;

use App\User;
use Illuminate\Support\Collection;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\Hidden;
use Laravel\Nova\Fields\Text;
use TuneZilla\DynamicActionFields\DynamicFieldAction;

/**
 * Class ChangeEmail
 * @package App\Nova\Actions
 *
 * Example usecase of tunezilla/dynamic-action-fields
 */
class ChangeEmail extends Action
{
    use DynamicFieldAction;

    public function fieldsForModels(Collection $models): array
    {
        if ($models->isEmpty()) {
            return [];
        }

        /** @var User $user */
        $user = $models->first();

        return [
            Text::make('Email')
                ->help('The new email for ' . $user->name),
            $user->hasVerifiedEmail()
                ? Boolean::make('Require Verification')
                    ->help('Send a verification email that they must accept')
                : Hidden::make('Require Verification'),
        ];
    }

    public function handle(ActionFields $fields, Collection $models)
    {
        /** @var User $user */
        $user = $models->first();
        $user->email = $fields->get('email');

        if ($fields->get('require_verification')) {
            $user->email_verified_at = null;
        }

        $user->save();

        if (!$user->hasVerifiedEmail()) {
            $user->sendEmailVerificationNotification();
        }

        return Action::message(implode(' ', [
            'Changed email to ' . $fields->get('email'),
            $fields->get('require_verification')
                ? 'and sent a verification email'
                : 'but did not send a verification email'
        ]));
    }
}

nova-dynamic-action-fields's People

Contributors

bsormagec avatar tunezilla avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.