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\BulkAction;
use App\Models\User;

// Delete inactive users
$execution = BulkAction::on(User::class)
    ->action('delete')
    ->where('status', 'inactive')
    ->where('last_login_at', '<', now()->subMonths(6))
    ->withUndo(days: 30)
    ->execute();

// Check progress
echo "UUID: {$execution->uuid}";
echo "Status: {$execution->status}";
echo "Progress: {$execution->progress_percentage}%";

$execution = BulkAction::on(User::class)
    ->action('archive')
    ->ids([1, 2, 3, 4, 5])
    ->with(['reason' => 'Account cleanup'])
    ->withUndo()
    ->execute();

$execution = BulkAction::on(User::class)
    ->action('update')
    ->where('role', 'subscriber')
    ->with(['data' => ['plan' => 'premium']])
    ->execute();

$execution = BulkAction::on(User::class)
    ->action('delete')
    ->where('status', 'inactive')
    ->dryRun()
    ->execute();

// Get preview data
$preview = $execution->dry_run_results;
echo "Would affect {$preview['total_count']} records";

$execution = BulkAction::on(User::class)
    ->action('delete')
    ->where('status', 'pending')
    ->scheduleFor('2024-12-31 00:00:00')
    ->execute();

$execution = BulkAction::on(User::class)
    ->action('update')
    ->ids([1, 2, 3])
    ->with(['data' => ['verified' => true]])
    ->sync() // Run immediately without queue
    ->execute();

return [
    'batch_size' => 500,          // Records per batch
    'queue' => [
        'connection' => null,      // Default queue connection
        'name' => 'default',
    ],
    'routes' => [
        'prefix' => 'bulk-actions', // API route prefix
        'middleware' => [
            'api' => ['api', 'auth:sanctum'],
        ],
    ],
    'undo' => [
        'enabled' => true,
        'default_expiry_days' => 7,
    ],
    'broadcasting' => [
        'enabled' => false,        // Enable WebSocket updates
    ],
    'audit' => [
        'enabled' => true,
    ],
    'rate_limiting' => [
        'enabled' => true,
        'max_concurrent_actions' => 5,
    ],
];

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();

'broadcasting' => [
    'enabled' => true,
    'channel_prefix' => 'bulk-action',
],
bash
php artisan action-engine:install