maintainedTool

Attempt for Laravel

Retry. Fallback. Recover.

Source ↗ (opens in a new tab)packagist.org ↗ (opens in a new tab)

Attempt: Retry. Fallback. Recover.

Overview

Retries, exponential backoff, and fallback chains for Laravel. Wrap a call, choose how it retries, and decide what happens if it fails.

Status
maintained
Languages
Laravel, PHP
Installcomposer require yannelli/attempt

LaravelPHPError HandlingRetry LogicResilienceFault Tolerance

Overview

Retry rules and fallbacks live right beside the call they protect. Wrap a closure or class, choose which failures to retry, and define a fallback for when retries run out.

Features

  • Retry selected exceptions with fixed delays, backoff, or jitter.
  • Run fallback handlers in order until one succeeds.
  • Hook into retries, success, and failure.
  • Wrap individual stages in Laravel’s Pipeline.
  • Retry transient provider failures with Attempt::ai() and the Laravel AI SDK.

Install

Requires PHP 8.4 or later and Laravel 12 or 13.

composer require yannelli/attempt

Optional config:

php artisan vendor:publish --tag="attempt-config"

Example

Here, PrimaryApi gets up to four tries, with exponential backoff between retries. If every call fails, BackupApi runs:

use Yannelli\Attempt\Facades\Attempt;

$result = Attempt::try(PrimaryApi::class)
    ->retry(3)
    ->exponentialBackoff(base: 100, max: 5000)
    ->fallback(BackupApi::class)
    ->thenReturn();

retry(3) allows three retries after the first call. Swap thenReturn() for run() to get an AttemptResult with the result, exception, and execution count.

Even more.

All work