Action Last updated: 2024-12-28

Sometimes, in some cases, we need to take an action

Then you will be confused how to do this, but be aware that it is a wonderful thing that we will be exposed to

Let me give you an example of the status of members
To activate and deactivate one or more members

First we need to create a new Action object

php artisan dash:make-action UserStatus
UserStatus Action generated

this is object  created in app\Dash\Actions path

<?php
namespace App\Dash\Actions;
use Dash\Extras\Inspector\Action;

class UserStatus extends Action {

	/**
	 * options to do some action with type message 
	 * like danger,info,warning,success
	 * @return array
	 */ 
	public static function options() {
		//Example
		return [
			'account_status' => [
				'active' => [
					'success' => 'user is activeated successfully',
   				],
				'deactivate' => [
					'danger' => 'deactivate successfully',
				],
				// or you can add custom label on your options 
				'custom_option' => [
					'label'=>'Your Custom label' // or you can use trans __('yourfile.key') || trans('yourfile.key') function to get custom locale dynamically 
					'danger' => 'deactivate successfully',
				] 
			],
		];
	}

}

// you can use this custom_option in your model observe to do something or changes
class YourModel extends Model
{

protected static function boot()
    {
        parent::boot();
        
        static::saving(function ($model) {
             $ids = request('ids'); // this is selected and merged as array  
       	     $action = json_decode(request('action')); // convert action json to array to get column and selected an custom_option value
        	 if (!empty($action->column) && !empty($action->value) && $action->column == 'account_status' && $action->value == 'custom_option' && !empty($ids) && count($ids) > 1) {
        	 	// do someting here
        	 	$model->your_custom_column = "your updated value";
        	 }
        });
 
    }
 }

 

columns => account_status

value (active , deactivate) 

message style you can use danger,info,warning,success 

success to show success message

warning to show warning message

info to show info message

danger to show danger message 

After the object is finished you can add it inside your resource in the actions method

public function actions() {
		return [
			\App\Dash\Actions\UserStatus::class,
		];
}