title | github | order |
---|---|---|
Actions |
6 |
Actions are runnable tasks on a given collection of models. Actions can be registered in resources and extracts. Also, actions are easily configurable and customizable for the current user.
You can generate new action classes by calling the root:action
artisan command:
php artisan root:action SendPasswordResetNotification
You can register actions in resources and extracts, by using the actions
method.
use App\Root\Actions\SendPasswordResetNotification;
use Illuminate\Http\Request;
/**
* Define the actions for the resource.
*/
public function actions(Request $request): array
{
return [
new SendPasswordResetNotification,
];
}
For the detailed documentation visit the fields section.
You may define fields for any action:
use Cone\Root\Actions\Action;
use Cone\Root\Fields\Date;
use Illuminate\Http\Request;
class Publish extends Action
{
/**
* Define the fields for the action.
*/
public function fields(Request $request): array
{
return [
Date::make('Published at')->withTime(),
];
}
}
When running the action, the submitted form data will be accessible in the
Request
object passed to the action'shandle
method.
You may allow or disallow interaction with actions. To do so, you can call the authorize
method on the action instance:
$action->authorize(static function (Request $request): bool {
return $request->user()->can('batchPublish', Post::class);
});
You may show or hide actions based on the current resource view. For example, some actions might be visible on the index
page, while others should be hidden. You can easily customize the action visibility logic using the visibleOn
and hiddenOn
methods:
$field->visibleOn(['index', 'show']);
$field->hiddenOn(['update', 'create']);
You may mark an action as destructive. That means, the Run
button that performs the action becomes red, indicating it is a dangerous action to run.
$action->destructive();
You may mark an action as confirmable. That means, when pressing the Run
button, the user receives a confirmation popup. If it's been cancelled, the action won't run.
$action->confirmable();
You may mark an action as standalone. That means, the action does not need any selected models. Also, the $models
collection in the handle
method will be empty.
$action->standalone();