Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build Laravel 10+ apps with Eloquent, Sanctum auth, Horizon queues, Livewire, and RESTful API resources.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
SKILL.md
1---2name: laravel-specialist3description: Build and configure Laravel 10+ applications, including creating Eloquent models and relationships, implementing Sanctum authentication, configuring Horizon queues, designing RESTful APIs with API resources, and building reactive interfaces with Livewire. Use when creating Laravel models, setting up queue workers, implementing Sanctum auth flows, building Livewire components, optimising Eloquent queries, or writing Pest/PHPUnit tests for Laravel features.4license: MIT5metadata:6author: https://github.com/Jeffallan7version: "1.1.0"8domain: backend9triggers: Laravel, Eloquent, PHP framework, Laravel API, Artisan, Blade templates, Laravel queues, Livewire, Laravel testing, Sanctum, Horizon10role: specialist11scope: implementation12output-format: code13related-skills: fullstack-guardian, test-master, devops-engineer, security-reviewer14---1516# Laravel Specialist1718Senior Laravel specialist with deep expertise in Laravel 10+, Eloquent ORM, and modern PHP 8.2+ development.1920## Core Workflow21221. **Analyse requirements** — Identify models, relationships, APIs, and queue needs232. **Design architecture** — Plan database schema, service layers, and job queues243. **Implement models** — Create Eloquent models with relationships, scopes, and casts; run `php artisan make:model` and verify with `php artisan migrate:status`254. **Build features** — Develop controllers, services, API resources, and jobs; run `php artisan route:list` to verify routing265. **Test thoroughly** — Write feature and unit tests; run `php artisan test` before considering any step complete (target >85% coverage)2728## Reference Guide2930Load detailed guidance based on context:3132| Topic | Reference | Load When |33|-------|-----------|-----------|34| Eloquent ORM | `references/eloquent.md` | Models, relationships, scopes, query optimization |35| Routing & APIs | `references/routing.md` | Routes, controllers, middleware, API resources |36| Queue System | `references/queues.md` | Jobs, workers, Horizon, failed jobs, batching |37| Livewire | `references/livewire.md` | Components, wire:model, actions, real-time |38| Testing | `references/testing.md` | Feature tests, factories, mocking, Pest PHP |3940## Constraints4142### MUST DO43- Use PHP 8.2+ features (readonly, enums, typed properties)44- Type hint all method parameters and return types45- Use Eloquent relationships properly (avoid N+1 with eager loading)46- Implement API resources for transforming data47- Queue long-running tasks48- Write comprehensive tests (>85% coverage)49- Use service containers and dependency injection50- Follow PSR-12 coding standards5152### MUST NOT DO53- Use raw queries without protection (SQL injection)54- Skip eager loading (causes N+1 problems)55- Store sensitive data unencrypted56- Mix business logic in controllers57- Hardcode configuration values58- Skip validation on user input59- Use deprecated Laravel features60- Ignore queue failures6162## Code Templates6364Use these as starting points for every implementation.6566### Eloquent Model6768```php69<?php7071declare(strict_types=1);7273namespace App\Models;7475use Illuminate\Database\Eloquent\Factories\HasFactory;76use Illuminate\Database\Eloquent\Model;77use Illuminate\Database\Eloquent\Relations\BelongsTo;78use Illuminate\Database\Eloquent\Relations\HasMany;79use Illuminate\Database\Eloquent\SoftDeletes;8081final class Post extends Model82{83use HasFactory, SoftDeletes;8485protected $fillable = ['title', 'body', 'status', 'user_id'];8687protected $casts = [88'status' => PostStatus::class, // backed enum89'published_at' => 'immutable_datetime',90];9192// Relationships — always eager-load via ::with() at call site93public function author(): BelongsTo94{95return $this->belongsTo(User::class, 'user_id');96}9798public function comments(): HasMany99{100return $this->hasMany(Comment::class);101}102103// Local scope104public function scopePublished(Builder $query): Builder105{106return $query->where('status', PostStatus::Published);107}108}109```110111### Migration112113```php114<?php115116use Illuminate\Database\Migrations\Migration;117use Illuminate\Database\Schema\Blueprint;118use Illuminate\Support\Facades\Schema;119120return new class extends Migration121{122public function up(): void123{124Schema::create('posts', function (Blueprint $table): void {125$table->id();126$table->foreignId('user_id')->constrained()->cascadeOnDelete();127$table->string('title');128$table->text('body');129$table->string('status')->default('draft');130$table->timestamp('published_at')->nullable();131$table->softDeletes();132$table->timestamps();133});134}135136public function down(): void137{138Schema::dropIfExists('posts');139}140};141```142143### API Resource144145```php146<?php147148declare(strict_types=1);149150namespace App\Http\Resources;151152use Illuminate\Http\Request;153use Illuminate\Http\Resources\Json\JsonResource;154155final class PostResource extends JsonResource156{157public function toArray(Request $request): array158{159return [160'id' => $this->id,161'title' => $this->title,162'body' => $this->body,163'status' => $this->status->value,164'published_at' => $this->published_at?->toIso8601String(),165'author' => new UserResource($this->whenLoaded('author')),166'comments' => CommentResource::collection($this->whenLoaded('comments')),167];168}169}170```171172### Queued Job173174```php175<?php176177declare(strict_types=1);178179namespace App\Jobs;180181use App\Models\Post;182use Illuminate\Bus\Queueable;183use Illuminate\Contracts\Queue\ShouldQueue;184use Illuminate\Foundation\Bus\Dispatchable;185use Illuminate\Queue\InteractsWithQueue;186use Illuminate\Queue\SerializesModels;187188final class PublishPost implements ShouldQueue189{190use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;191192public int $tries = 3;193public int $backoff = 60;194195public function __construct(196private readonly Post $post,197) {}198199public function handle(): void200{201$this->post->update([202'status' => PostStatus::Published,203'published_at' => now(),204]);205}206207public function failed(\Throwable $e): void208{209// Log or notify — never silently swallow failures210logger()->error('PublishPost failed', ['post' => $this->post->id, 'error' => $e->getMessage()]);211}212}213```214215### Feature Test (Pest)216217```php218<?php219220use App\Models\Post;221use App\Models\User;222223it('returns a published post for authenticated users', function (): void {224$user = User::factory()->create();225$post = Post::factory()->published()->for($user, 'author')->create();226227$response = $this->actingAs($user)228->getJson("/api/posts/{$post->id}");229230$response->assertOk()231->assertJsonPath('data.status', 'published')232->assertJsonPath('data.author.id', $user->id);233});234235it('queues a publish job when a draft is submitted', function (): void {236Queue::fake();237$user = User::factory()->create();238$post = Post::factory()->draft()->for($user, 'author')->create();239240$this->actingAs($user)241->postJson("/api/posts/{$post->id}/publish")242->assertAccepted();243244Queue::assertPushed(PublishPost::class, fn ($job) => $job->post->is($post));245});246```247248## Validation Checkpoints249250Run these at each workflow stage to confirm correctness before proceeding:251252| Stage | Command | Expected Result |253|-------|---------|-----------------|254| After migration | `php artisan migrate:status` | All migrations show `Ran` |255| After routing | `php artisan route:list --path=api` | New routes appear with correct verbs |256| After job dispatch | `php artisan queue:work --once` | Job processes without exception |257| After implementation | `php artisan test --coverage` | >85% coverage, 0 failures |258| Before PR | `./vendor/bin/pint --test` | PSR-12 linting passes |259260## Knowledge Reference261262Laravel 10+, Eloquent ORM, PHP 8.2+, API resources, Sanctum/Passport, queues, Horizon, Livewire, Inertia, Octane, Pest/PHPUnit, Redis, broadcasting, events/listeners, notifications, task scheduling263264[Documentation](https://jeffallan.github.io/claude-skills/skills/backend/laravel-specialist/)265