PHP code example of spatie / laravel-queueable-action
1. Go to this page and download the library: Download spatie/laravel-queueable-action 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/ */
spatie / laravel-queueable-action example snippets
return [
/*
* The job class that will be dispatched.
* If you would like to change it and use your own job class,
* it must extends the \Spatie\QueueableAction\ActionJob class.
*/
'job_class' => \Spatie\QueueableAction\ActionJob::class,
];
class MyController
{
public function store(
MyRequest $request,
MyModel $model,
MyAction $action
) {
$requestData = RequestData::fromRequest($myRequest);
// Execute the action on the queue:
$action->onQueue()->execute($model, $requestData);
// Or right now:
$action->execute($model, $requestData);
}
}
/** @test */
public function it_queues_an_action()
{
Queue::fake();
(new DoSomethingAction)->onQueue()->execute();
QueueableActionFake::assertPushed(DoSomethingAction::class);
}
php
class MyAction
{
use QueueableAction;
public function __construct(
OtherAction $otherAction,
ServiceFromTheContainer $service
) {
// Constructor arguments can come from the container.
$this->otherAction = $otherAction;
$this->service = $service;
}
public function execute(
MyModel $model,
RequestData $requestData
) {
// The business logic goes here, this can be executed in an async job.
}
}
php
class CustomTagsAction
{
use QueueableAction;
// ...
public function tags() {
return ['action', 'custom_tags'];
}
}
php
class BackoffAction
{
use QueueableAction;
/**
* Calculate the number of seconds to wait before retrying the action.
*
*/
public function backoff(): int
{
return 3;
}
}
php
class BackoffAction
{
/**
* Calculate the number of seconds to wait before retrying the action.
*
*/
public function backoff(): array
{
return [1, 5, 10];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.