Header Ads

How to set action without select an option on Laravel Nova

Nova is the best admin panel that uses the Laravel framework to build, allowing you to perform custom tasks on one or more Eloquent models. 

1. Laravel Nova is a free open-source content management system (CMS) based on PHP and MySQL. It was developed by Laravel Forge.

2. Laravel Nova is built on top of the Laravel 5.5 framework.

3. Laravel Nova has been designed from scratch to provide a simple yet powerful toolset that allows developers to build their own CMS without having to learn how to code.

4. Laravel Nova is written in PHP 7.0+ and uses Composer for dependency management.

5. Laravel Nova supports both MySQL and PostgreSQL databases.

6. Laravel Nova provides a set of features that are not commonly found in other CMSs including:

This tutorial focuses on users' active and inactive when the User checks active it will active but check inactive it will inactive. 

In this case, we will learn how to set and use the stand-alone button action on nova to perform some records. 


1. An action without a stand-alone button 

The first time create an action what is your favorite name my action name is UnpublishedUser extent from Action. Inside the class, we use InteractsWithQueue, and Queueable. to perform the action on the giving model. we use the function to handle that use some parameter ActionFields $fields, Collection $models.
After that, we take a look at each loop to update Action by a given model. It will perform all row fields if we select only one it will do only one if we multi-select it also does all. and then I will return the action with the message. 

    <?PHP

    namespace App\Nova\Actions;

    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Support\Collection;
    use Laravel\Nova\Actions\Action;
    use Laravel\Nova\Fields\ActionFields;

    class UnpublishedUser extends Action
    {
        use InteractsWithQueueQueueable;
    

        /**
         * Perform the action on the given models.
         *
         * @param  \Laravel\Nova\Fields\ActionFields  $fields
         * @param  \Illuminate\Support\Collection  $models
         @return mixed
         */
        public function handle(ActionFields $fieldsCollection $models)
        {
            foreach($models as $model){
                $model->update([
                    'active'=>'No'
                ]);
            }
            return Action::message("You have been Unpublished.");
            }

        /**
         * Get the fields available on the action.
         *
         * @return array
         */
        public function fields()
        {
            return [];
        }
    }


2. Button Stand alone action 

The first time creates an action what is your favorite name like my action name is UnpublishedUser extent from Action. Inside the class, we use InteractsWithQueue, and Queueable. to perform the action on the giving model. 
We use the function to handle that use some parameter ActionFields $fields, Collection $models.
After that, we take a look at each loop to update Action by a given model. It will perform all row fields if we select only one it will do only one if we multi-select it also does all. and then return the action with the message. and then at the main point is  
Go to your action class and type add more line public $standalone = true;

    <?PHP

    namespace App\Nova\Actions;

    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Support\Collection;
    use Laravel\Nova\Actions\Action;
    use Laravel\Nova\Fields\ActionFields;

    class UnpublishedUser extends Action
    {
        use InteractsWithQueueQueueable;
        public $standalone = true;

        /**
         * Perform the action on the given models.
         *
         @param  \Laravel\Nova\Fields\ActionFields  $fields
         @param  \Illuminate\Support\Collection  $models
         @return mixed
         */
        public function handle(ActionFields $fieldsCollection $models)
        {
            foreach($models as $model){
                $model->update([
                    'active'=>'No'
                ]);
            }
            return Action::message("You have been Unpublished.");
        }

    
        /**
         * Get the fields available on the action.
         *
         * @return array
         */
        public function fields()
        {
            return [];
        }
    }

No comments:

Powered by Blogger.