Download the PHP package laragear/attempt-once without Composer
On this page you can find all versions of the php package laragear/attempt-once. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download laragear/attempt-once
More information about laragear/attempt-once
Files in laragear/attempt-once
Package attempt-once
Short Description Run and manage callbacks across multiple app instances
License MIT
Informations about the package attempt-once
Deprecated
Use Cache::funnel() with 1 attempt instead.
Once
Run and manage callbacks across multiple app instances, atomically.
Become a sponsor
Your support allows me to keep this package free, up-to-date and maintainable.
Requirements
- PHP 8.4
- Laravel 11 or later
Installation
You can install the package via Composer.
How does this work?
It uses Laravel's Rate Limiter behind the scenes to allow callbacks to run once. This way all app instances are aware of the callback execution, ensuring only runs once inside a given window of time, which is 1 minute by default.
Usage
The simplest way to use attempt_once() is to just issue a callback, which will be executed only once for 30 seconds. A key will be computed from the callback as laragear|attempt_once:{hash} based on the callback unique position in your application.
Adding a second argument after the callback will set the amount of time to "hold" the execution. You may use a DateTimeInterface instance, a DateInterval object or just an amount of seconds.
[!IMPORTANT]
When using a single callback, the hash computed takes into account the place where is called. Duplicating the code will make both callbacks hashes different, even if they do exactly the same.
While the above may suffice for most simple scenarios, you may have more granular control on the execution by calling attempt_once() with a key to identify the execution, or a string backed enum. You will receive a builder instance to configure how to run it:
Custom key
The attempt_once() method allows to identify the callback to run once with a simple string. With a constant string, you may check the execution status elsewhere in your app.
You can issue multiple strings or arrays, which is great when you need to separate executions programmatically. The resulting key will be concatenated using |. If you pass an array, the key and value will be concatenated as {key}:{value}.
If you pass an Eloquent Model as second parameter, it will be used to identify the callback as {class}:{key}, essentially making the callback unique for each model you pass.
When using an Enum as key, the class name and case name will be used as part of the key as {class}:{case}.
Time
You may change the window of time using for() with the amount of seconds. You may also use DateTimeInterface instance, or a DateInterval instance.
[!IMPORTANT]
You may change the window of time programmatically, but consider that only previous successful execution will set the time. Since subsequent executions won't run, the time won't be updated.
Callback result
When the callback runs, its result will be returned. When the callback does not run because it's rate limited, false will be returned.
Default result
Sometimes you will want to return another value rather than false if the callback is not executed. For that, use the or() method.
Checking execution
To check if the callback was executed, you may use the wasExecuted() and wasNotExecuted() methods, which is only accessible using a named key.
To check how many seconds remain to execute the callback again, use availableIn(), or readyAt() to get a Carbon instance.
Cache Store
By default, Laravel's Rate Limiter uses the application default cache store. If you want to change the cache store to use, configure the Rate Limiter in your app configuration.
Laravel Octane compatibility
- There are no singletons using a stale app instance.
- There are no singletons using a stale config instance.
- There are no singletons using a stale request instance.
- There are no static properties written during a request.
There should be no problems using this package with Laravel Octane.
Security
If you discover any security-related issues, issue a Security Advisory.
License
This specific package version is licensed under the terms of the MIT License, at the time of publishing.
Laravel is a Trademark of Taylor Otwell. Copyright © 2011-2026 Laravel LLC.