PHP code example of dhruvilnagar / laravel-action-engine
1. Go to this page and download the library: Download dhruvilnagar/laravel-action-engine library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dhruvilnagar / laravel-action-engine example snippets
use DhruvilNagar\ActionEngine\Facades\ActionRegistry;
public function boot()
{
// Simple closure-based action
ActionRegistry::register('send_email', function ($record, $params) {
Mail::to($record->email)->send(new BulkEmail($params['message']));
return true;
}, [
'label' => 'Send Email',
'supports_undo' => false,
]);
// Class-based action
ActionRegistry::register('notify', NotifyAction::class);
}
use DhruvilNagar\ActionEngine\Contracts\ActionInterface;
use Illuminate\Database\Eloquent\Model;
class NotifyAction implements ActionInterface
{
public function execute(Model $record, array $parameters = []): bool
{
$record->notify(new BulkNotification($parameters['message']));
return true;
}
public function getName(): string
{
return 'notify';
}
public function getLabel(): string
{
return 'Send Notification';
}
public function supportsUndo(): bool
{
return false;
}
public function getUndoType(): ?string
{
return null;
}
public function validateParameters(array $parameters): array
{
return $parameters;
}
public function getUndoFields(): array
{
return [];
}
public function afterComplete(array $results): void
{
// Cleanup or notification logic
}
}
// In your component
<livewire:action-engine.bulk-action-manager
:model="App\Models\User::class"
:selected-ids="$selectedIds"
/>
use App\Filament\Actions\BulkDeleteAction;
use App\Filament\Actions\BulkArchiveAction;
public function table(Table $table): Table
{
return $table
->bulkActions([
BulkDeleteAction::make(),
BulkArchiveAction::make(),
]);
}
use DhruvilNagar\ActionEngine\Traits\HasBulkActions;
class User extends Model
{
use HasBulkActions;
}
// Now you can use:
User::bulkDelete([1, 2, 3]);
User::bulkUpdate([1, 2, 3], ['status' => 'active']);
User::bulkArchive([1, 2, 3], 'Cleanup');
User::getBulkActionHistory();
User::getUndoableBulkActions();