Laravel actions

From Inikoo-Ltd/aiku · View interactive version

Laravel actions is an AI agent skill from the GitHub repository Inikoo-Ltd/aiku. Build, refactor, and troubleshoot Laravel Actions using lorisleiva/laravel-actions. Use when implementing reusable action classes (object/controller/job/listener/command), converting service classes/controllers/jobs into actions, orchestrating workflows via faked actions, or debugging action entrypo

How to use this skill

Skill file

Laravel Actions or `lorisleiva/laravel-actions`

Overview

Use this skill to implement or update actions based on lorisleiva/laravel-actions with consistent structure and predictable testing patterns.

Quick Workflow

  1. Confirm the package is installed with composer show lorisleiva/laravel-actions.
  2. Create or edit an action class that uses Lorisleiva\Actions\Concerns\AsAction.
  3. Implement handle(...) with the core business logic first.
  4. Add adapter methods only when needed for the requested entrypoint:
  1. Add or update tests for the chosen entrypoint.
  2. When tests need isolation, use action fakes (MyAction::fake()) and assertions (MyAction::assertDispatched()).

Base Action Pattern

Use this minimal skeleton and expand only what is needed.

<?php

namespace App\Actions;

use Lorisleiva\Actions\Concerns\AsAction;

class PublishArticle
{
    use AsAction;

    public function handle(int $articleId): bool
    {
        return true;
    }
}

Project Conventions

When to Use an Action

Entrypoint Patterns

Run as Object

Run as Controller

Run as Job

Project Pattern: Job Action with Extra Methods
<?php

namespace App\Actions\Demo;

use App\Models\Demo;
use DateTime;
use Lorisleiva\Actions\Concerns\AsAction;
use Lorisleiva\Actions\Decorators\JobDecorator;

class GetDemoData
{
    use AsAction;

    public int $jobTries = 3;

    public int $jobMaxExceptions = 3;

    public function getJobRetryUntil(): DateTime
    {
        return now()->addMinutes(30);
    }

    public function getJobBackoff(): array
    {
        return [60, 120];
    }

    public function getJobUniqueId(Demo $demo): string
    {
        return $demo->id;
    }

    public function handle(Demo $demo): void
    {
        // Core business logic.
    }

    public function asJob(JobDecorator $job, Demo $demo): void
    {
        // Queue-specific orchestration and retry behavior.
        $this->handle($demo);
    }
}

Use these members only when needed:

Run as Listener

Run as Command

Testing Guidance

Use a two-layer strategy:

  1. handle(...) tests for business correctness.
  2. entrypoint tests (asController, asJob, asListener, asCommand) for wiring/orchestration.

Deep Dive: `AsFake` methods (2.x)

Reference: https://www.laravelactions.com/2.x/as-fake.html

Use these methods intentionally based on what you want to prove.

`mock()`
PublishArticle::mock()
    ->shouldReceive('handle')
    ->once()
    ->with(42)
    ->andReturnTrue();
`partialMock()`
PublishArticle::partialMock()
    ->shouldReceive('fetchRemoteData')
    ->once()
    ->andReturn(['ok' => true]);
`spy()`
$spy = PublishArticle::spy()->allows('handle')->andReturnTrue();

// execute code that triggers the action...

$spy->shouldHaveReceived('handle')->with(42);
`shouldRun()`
PublishArticle::shouldRun()->once()->with(42)->andReturnTrue();
`shouldNotRun()`
PublishArticle::shouldNotRun();
`allowToRun()`
$spy = PublishArticle::allowToRun()->andReturnTrue();
// ...
$spy->shouldHaveReceived('handle')->once();
`isFake()` and `clearFake()`
expect(PublishArticle::isFake())->toBeFalse();
PublishArticle::mock();
expect(PublishArticle::isFake())->toBeTrue();
PublishArticle::clearFake();
expect(PublishArticle::isFake())->toBeFalse();

Recommended test matrix for Actions

Practical defaults

Pest style examples

it('dispatches the downstream action', function () {
    SendInvoiceEmail::shouldRun()->once()->withArgs(fn (int $invoiceId) => $invoiceId > 0);

    FinalizeInvoice::run(123);
});

it('does not dispatch when invoice is already sent', function () {
    SendInvoiceEmail::shouldNotRun();

    FinalizeInvoice::run(123, alreadySent: true);
});

Run the minimum relevant suite first, e.g. php artisan test --compact --filter=PublishArticle or by specific test file.

Troubleshooting Checklist

Common Pitfalls

Topic References

Use these references for deep dives by entrypoint/topic. Keep SKILL.md focused on workflow and decision rules.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.