Páginas · Laravilt
Custom Action
Build your own actions inline or as reusable classes.
With a form
URL actions
An action with a URL navigates instead of running a closure:
Closure arguments
Arguments are injected by parameter name:
| Parameter | Value |
|---|---|
$record | The current record |
$data | Submitted modal form values |
$records | Selected records, as a collection (bulk actions) |
$ids | Selected record keys (bulk actions) |
Get $get, Set $set | Laravilt\Support\Utilities\Get / Set for form data |
Reusable action classes
Generate a class with make:action:
--auth combines with --modal or --form. If you pass both --modal and --form, --form wins. --force overwrites an existing file.
The generated
handle()wiring and the--authoption require Laravilt v1.1 or later.
The generated class calls handle() from action(), so you only need to fill in handle():
$data holds the submitted form data (with --form). By default authorizeAction() only checks that the user is logged in, so replace it with your own check, such as a policy. When it returns false, the action is hidden and its endpoint responds with a 403. See Authorization.
You can also write the class by hand:
The class goes in app/Actions and extends Laravilt\Actions\Action. Configure it in setUp() and wire your logic with action():
The generated stub includes a
handle()method, but Laravilt doesn't call it automatically. Call it from youraction()closure, or put the logic in the closure directly as shown above.
API reference
| Method | Description |
|---|---|
make(?string $name) | Create the action |
action(Closure) | Server-side handler |
url(), openUrlInNewTab() | Navigate instead |
label(), icon(), color() | See Styling |
requiresConfirmation(), modalHeading(), slideOver() | See Confirmation |
schema() / form() | See Forms |
can(), authorize(), visible() | See Authorization |