PHP code example of truschery / idem

1. Go to this page and download the library: Download truschery/idem 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/ */

    

truschery / idem example snippets



// Routes
Route::post('/payments', [PaymentController::class, 'store'])
    ->middleware('idempotent');

use Truschery\Idem\Middleware\EnsureIdempotent;

class ProcessPaymentJob implements ShouldQueue
{
    public function __construct(private string $paymentId) {}

    public function middleware(): array
    {
        return [new EnsureIdempotent($this->paymentId)];
    }

    public function handle(): void
    {
        // Runs only once, even if the job is dispatched multiple times
        Payment::process($this->paymentId);
    }
}

use Truschery\Idem\Once;

$result = Once::do('send-welcome-email:' . $user->id, function () use ($user) {
    return Mail::to($user)->send(new WelcomeMail($user));
});
bash
php artisan vendor:publish --tag=idem-config
bash
php artisan vendor:publish --tag=idem-migrations
php artisan migrate