PHP code example of bugfix666 / laravel-outbox
1. Go to this page and download the library: Download bugfix666/laravel-outbox 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/ */
bugfix666 / laravel-outbox example snippets
return [
'table' => env('OUTBOX_TABLE', 'outbox_messages'),
'connection' => env('OUTBOX_DB_CONNECTION', env('DB_CONNECTION', 'mysql')),
// Your custom publisher class (must implement OutboxPublisher)
'publisher' => env('OUTBOX_PUBLISHER', Bugfix666\LaravelOutbox\Publishers\LogPublisher::class),
'worker' => [
'batch_size' => env('OUTBOX_BATCH_SIZE', 100),
'lock_timeout' => env('OUTBOX_LOCK_TIMEOUT', 10), // seconds
'lock_store' => env('OUTBOX_LOCK_STORE', 'cache'), // cache, redis, etc.
],
'cleanup' => [
'days' => env('OUTBOX_CLEANUP_DAYS', 7),
],
'retry' => [
'max_attempts' => env('OUTBOX_MAX_ATTEMPTS', 5),
'delay_seconds' => env('OUTBOX_RETRY_DELAY', 60), // seconds between attempts
],
];
use Bugfix666\LaravelOutbox\Services\OutboxService;
use Illuminate\Support\Facades\DB;
DB::transaction(function () use ($outboxService) {
// 1. Business logic (e.g., create a payment)
$payment = Payment::create([
'amount' => 1000,
'currency' => 'USD',
'status' => 'completed',
]);
// 2. Store the outbox message – guaranteed to be persisted atomically
$outboxService->store(
aggregateType: 'Payment',
aggregateId: (string) $payment->id,
eventType: 'payment.completed',
payload: [
'payment_id' => $payment->id,
'amount' => $payment->amount,
'user_id' => auth()->id(),
]
);
});
use Illuminate\Support\Facades\Schedule;
Schedule::command('outbox:cleanup')->daily();
namespace App\Publishers;
use Bugfix666\LaravelOutbox\Contracts\OutboxPublisher;
use RdKafka\Producer;
class KafkaPublisher implements OutboxPublisher
{
public function __construct(private Producer $producer) {}
public function publish(string $eventType, array $payload, ?string $aggregateId = null): bool
{
$topic = $this->producer->newTopic($eventType);
$topic->produce(RD_KAFKA_PARTITION_UA, 0, json_encode($payload));
$this->producer->flush(1000);
return true;
}
}
use Bugfix666\LaravelOutbox\Events\OutboxMessageFailed;
class LogOutboxFailure
{
public function handle(OutboxMessageFailed $event): void
{
logger()->error('Outbox failure', [
'id' => $event->message->id,
'attempts' => $event->message->attempts,
'error' => $event->exception->getMessage(),
]);
}
}
public function test_message_is_stored(): void
{
DB::transaction(function () {
$service = app(OutboxService::class);
$message = $service->store('User', '123', 'user.created', ['name' => 'John']);
$this->assertDatabaseHas('outbox_messages', ['id' => $message->id]);
});
}
bash
php artisan vendor:publish --tag=outbox-config
php artisan vendor:publish --tag=outbox-migrations
bash
php artisan migrate
bash
php artisan outbox:process
bash
php artisan outbox:process --batch-size=200
bash
php artisan outbox:cleanup --days=14