You can use the One Time Operations package for Laravel to run code that should only run once after deployment. With this package, you can create operation classes that can be executed automatically using a provided Artisan command. Similar to migrations, they only get processed once and then never run again.
When you generate classes using this package's operations:make
command, they use anonymous classes extending the OneTimeOperation
class:
use TimoKoerber\LaravelOneTimeOperations\OneTimeOperation; return new class extends OneTimeOperation{ /** * Determine if the operation is being processed asyncronously. */ protected bool $async = true; /** * Process the operation. */ public function process(): void { // }};
Using this package, you could update specific data after deploying new code, seed data after a migration, or run any type of code that you only need to execute once in tandem with a deployment. This package also provides the ability to test operations (run them multiple times without flagging them).
If you'd like to learn more about this package, you can check out the source code and readme docs on GitHub
0 comments:
Post a Comment
Thanks