Laravel app specialist

From findscripter/everything-skills · View interactive version

Laravel app specialist is an AI agent skill from the GitHub repository findscripter/everything-skills. 当用 Laravel 10+/PHP 8.2+ 构建应用时使用;做 Eloquent 模型与关系、迁移、RESTful API(API Resource)、Sanctum 鉴权、队列/Horizon、Livewire 组件与 Pest/PHPUnit 测试并产出生产级代码;不适用于纯 PHP 语法、其他框架(Symfony/裸 PHP)、纯前端或纯 SQL 调优。触发词:Laravel、Eloquent、Artisan、Sanctum、Horizon、Livewire

How to use this skill

Skill file

何时使用

适用:

不该用(负边界):

步骤

源仓库的核心工作流(适配):

  1. 分析需求:识别模型、关系、API 与队列需求。
  2. 设计架构:规划数据库 schema、服务层与 Job 队列边界。
  3. 实现模型:建带关系/作用域/casts 的 Eloquent 模型;php artisan make:model,用 php artisan migrate:status 验证。
  4. 构建特性:控制器、服务、API Resource、Job;用 php artisan route:list 验证路由。
  5. 充分测试:写 feature/unit 测试,每步收尾前先跑 php artisan test(目标覆盖率 >85%)。

指令

必须做(MUST DO):

禁止做(MUST NOT DO):

各阶段验证检查点(在进入下一步前确认):

| 阶段 | 命令 | 期望 |

|---|---|---|

| 迁移后 | php artisan migrate:status | 全部显示 Ran |

| 路由后 | php artisan route:list --path=api | 新路由出现且动词正确 |

| 派发 Job 后 | php artisan queue:work --once | Job 无异常处理完 |

| 实现后 | php artisan test --coverage | 覆盖率 >85%,0 失败 |

| 提 PR 前 | ./vendor/bin/pint --test | PSR-12 检查通过 |

示例

Eloquent 模型(关系 + 作用域 + enum cast,关系在调用点 ::with() 预加载):

<?php
declare(strict_types=1);
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\{BelongsTo, HasMany};
use Illuminate\Database\Eloquent\SoftDeletes;

final class Post extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = ['title', 'body', 'status', 'user_id'];
    protected $casts = [
        'status'       => PostStatus::class,   // backed enum
        'published_at' => 'immutable_datetime',
    ];

    public function author(): BelongsTo { return $this->belongsTo(User::class, 'user_id'); }
    public function comments(): HasMany { return $this->hasMany(Comment::class); }

    public function scopePublished(Builder $query): Builder
    {
        return $query->where('status', PostStatus::Published);
    }
}

API Resource(用 whenLoaded 避免触发 N+1):

public function toArray(Request $request): array
{
    return [
        'id'           => $this->id,
        'title'        => $this->title,
        'status'       => $this->status->value,
        'published_at' => $this->published_at?->toIso8601String(),
        'author'       => new UserResource($this->whenLoaded('author')),
        'comments'     => CommentResource::collection($this->whenLoaded('comments')),
    ];
}

队列 Job(重试 + failed() 不吞异常):

final class PublishPost implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public int $tries = 3;
    public int $backoff = 60;

    public function __construct(private readonly Post $post) {}

    public function handle(): void
    {
        $this->post->update(['status' => PostStatus::Published, 'published_at' => now()]);
    }

    public function failed(\Throwable $e): void
    {
        logger()->error('PublishPost failed', ['post' => $this->post->id, 'error' => $e->getMessage()]);
    }
}

Pest feature 测试(鉴权访问 + 断言队列派发):

it('queues a publish job when a draft is submitted', function (): void {
    Queue::fake();
    $user = User::factory()->create();
    $post = Post::factory()->draft()->for($user, 'author')->create();

    $this->actingAs($user)
        ->postJson("/api/posts/{$post->id}/publish")
        ->assertAccepted();

    Queue::assertPushed(PublishPost::class, fn ($job) => $job->post->is($post));
});

迁移要点:foreignId('user_id')->constrained()->cascadeOnDelete() + softDeletes() + timestamps(),down() 用 Schema::dropIfExists()。

注意事项

互见


采编自 jeffallan/claude-skills(MIT 许可)。

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.