laravel-specialist

laravel-specialist

热门

构建和配置 Laravel 10+ 应用程序,包括创建 Eloquent 模型和关系、实现 Sanctum 认证、配置 Horizon 队列、使用 API 资源设计 RESTful API,以及使用 Livewire 构建响应式界面。在创建 Laravel 模型、设置队列工作者、实现 Sanctum 认证流程、构建 Livewire 组件、优化 Eloquent 查询或为 Laravel 功能编写 Pest/PHPUnit 测试时使用。

1万Star
907Fork
更新于 2026/5/20
SKILL.md
只读
名称
laravel-specialist
描述

构建和配置 Laravel 10+ 应用程序,包括创建 Eloquent 模型和关系、实现 Sanctum 认证、配置 Horizon 队列、使用 API 资源设计 RESTful API,以及使用 Livewire 构建响应式界面。在创建 Laravel 模型、设置队列工作者、实现 Sanctum 认证流程、构建 Livewire 组件、优化 Eloquent 查询或为 Laravel 功能编写 Pest/PHPUnit 测试时使用。

Laravel 专家

资深 Laravel 专家,精通 Laravel 10+、Eloquent ORM 和现代 PHP 8.2+ 开发。

核心工作流程

  1. 分析需求 — 确定模型、关系、API 和队列需求
  2. 设计架构 — 规划数据库模式、服务层和任务队列
  3. 实现模型 — 创建带有关系、作用域和类型转换的 Eloquent 模型;运行 php artisan make:model 并通过 php artisan migrate:status 验证
  4. 构建功能 — 开发控制器、服务、API 资源和任务;运行 php artisan route:list 验证路由
  5. 全面测试 — 编写功能和单元测试;在认为任何步骤完成前运行 php artisan test(目标覆盖率 >85%)

参考指南

根据上下文加载详细指导:

主题 参考 加载时机
Eloquent ORM references/eloquent.md 模型、关系、作用域、查询优化
路由与 API references/routing.md 路由、控制器、中间件、API 资源
队列系统 references/queues.md 任务、工作者、Horizon、失败任务、批处理
Livewire references/livewire.md 组件、wire:model、操作、实时
测试 references/testing.md 功能测试、工厂、模拟、Pest PHP

约束

必须做

  • 使用 PHP 8.2+ 特性(readonly、枚举、类型化属性)
  • 对所有方法参数和返回类型进行类型提示
  • 正确使用 Eloquent 关系(通过预加载避免 N+1)
  • 使用 API 资源转换数据
  • 对长时间运行的任务进行队列化
  • 编写全面的测试(覆盖率 >85%)
  • 使用服务容器和依赖注入
  • 遵循 PSR-12 编码标准

禁止做

  • 使用未受保护的原始查询(SQL 注入)
  • 跳过预加载(导致 N+1 问题)
  • 未加密存储敏感数据
  • 在控制器中混合业务逻辑
  • 硬编码配置值
  • 跳过用户输入验证
  • 使用已弃用的 Laravel 特性
  • 忽略队列失败

代码模板

将这些作为每个实现的起点。

Eloquent 模型

<?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;
use Illuminate\Database\Eloquent\Relations\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, // 后端枚举
        'published_at' => 'immutable_datetime',
    ];

    // 关系 — 始终在调用处通过 ::with() 预加载
    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);
    }
}

迁移

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('posts', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('title');
            $table->text('body');
            $table->string('status')->default('draft');
            $table->timestamp('published_at')->nullable();
            $table->softDeletes();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('posts');
    }
};

API 资源

<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

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

队列任务

<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Models\Post;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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)

<?php

use App\Models\Post;
use App\Models\User;

it('returns a published post for authenticated users', function (): void {
    $user = User::factory()->create();
    $post = Post::factory()->published()->for($user, 'author')->create();

    $response = $this->actingAs($user)
        ->getJson("/api/posts/{$post->id}");

    $response->assertOk()
        ->assertJsonPath('data.status', 'published')
        ->assertJsonPath('data.author.id', $user->id);
});

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));
});

验证检查点

在每个工作流阶段运行以下命令以确认正确性:

阶段 命令 预期结果
迁移后 php artisan migrate:status 所有迁移显示 Ran
路由后 php artisan route:list --path=api 新路由出现且动词正确
任务分发后 php artisan queue:work --once 任务无异常处理
实现后 php artisan test --coverage 覆盖率 >85%,0 失败
PR 前 ./vendor/bin/pint --test PSR-12 代码规范检查通过

知识参考

Laravel 10+, Eloquent ORM, PHP 8.2+, API 资源, Sanctum/Passport, 队列, Horizon, Livewire, Inertia, Octane, Pest/PHPUnit, Redis, 广播, 事件/监听器, 通知, 任务调度

文档