Laravel

From TerminalSkills/skills · View interactive version

Laravel is an AI agent skill from the GitHub repository TerminalSkills/skills. Laravel is a PHP web framework for building full-stack applications and JSON APIs with Eloquent ORM, Blade views, the Artisan CLI, queues, events and Sanctum authentication. Use when a user asks to create a Laravel app, add models, migrations, controllers, form requests, API routes, queued jobs or t

How to use this skill

Skill file

Laravel — The PHP Framework for Web Artisans

Overview

Laravel is a full-stack PHP framework. Laravel 13 (released March 2026, current line 13.x) requires PHP 8.3 or newer. It ships Eloquent (ORM), Blade (templates), Artisan (CLI), a queue system, events, scheduling and first-party packages such as Sanctum (API tokens and SPA cookies), Fortify (headless auth), Livewire, Horizon and Pint. New apps use SQLite by default, so laravel new works with no database server.

Instructions

Create and run an app

composer global require laravel/installer     # needs PHP 8.3+ and Composer
laravel new orders-api                         # prompts for a starter kit, tests, database
cd orders-api
npm install && npm run build
composer run dev                               # serves :8000, queue worker and Vite together

Eloquent models

$fillable, $hidden and casts() on the model work in every version; Laravel 13 also accepts PHP attributes such as #[Fillable([...])] and #[Table('...')]. Define casts in a casts() method. Mark local scopes with #[Scope].

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Order extends Model
{
    use SoftDeletes;

    protected $fillable = ['user_id', 'reference', 'status', 'total_cents'];

    protected function casts(): array
    {
        return ['shipped_at' => 'datetime', 'meta' => 'array'];
    }

    public function items(): HasMany
    {
        return $this->hasMany(OrderItem::class);
    }

    #[Scope]
    protected function pending(Builder $query): void
    {
        $query->where('status', 'pending');
    }
}
// Order::pending()->with('items')->latest()->paginate(20);

Validation and controllers

Put validation in a FormRequest (php artisan make:request StoreOrderRequest) and keep controllers thin. Hash passwords with the hashed cast on the User model (the default skeleton has it) or Hash::make.

// app/Http/Controllers/OrderController.php
public function store(StoreOrderRequest $request)
{
    $order = $request->user()->orders()->create($request->validated());
    ProcessOrder::dispatch($order)->onQueue('orders');

    return new OrderResource($order);   // 201 for a newly created model
}

API routes and Sanctum

routes/api.php does not exist in a fresh app. Run php artisan install:api to publish it, install Sanctum and add the migration, then migrate.

php artisan install:api
php artisan migrate
// routes/api.php
Route::post('/sanctum/token', function (Request $request) {
    $request->validate(['email' => 'required|email', 'password' => 'required', 'device_name' => 'required']);
    $user = User::where('email', $request->email)->first();
    if (! $user || ! Hash::check($request->password, $user->password)) {
        throw ValidationException::withMessages(['email' => ['The provided credentials are incorrect.']]);
    }
    return $user->createToken($request->device_name, ['orders:read'])->plainTextToken;
});

Route::middleware('auth:sanctum')->group(function () {
    Route::apiResource('orders', OrderController::class);
});

The User model needs the Laravel\Sanctum\HasApiTokens trait. Clients send Authorization: Bearer <token>. Tokens are hashed in the database, so show plainTextToken once. For a first-party SPA do not use tokens: call $middleware->statefulApi() in bootstrap/app.php, set SANCTUM_STATEFUL_DOMAINS, fetch /sanctum/csrf-cookie, then POST /login.

Queues

<?php
namespace App\Jobs;

use App\Models\Order;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class ProcessOrder implements ShouldQueue
{
    use Queueable;

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

    public function __construct(public Order $order) {}

    public function handle(): void
    {
        $this->order->process();
    }
}

Run php artisan queue:work (or let composer run dev do it). The default queue driver is database; use Redis plus Horizon for volume. Restart workers after a deploy with php artisan queue:restart.

Laravel 13 notes (upgrading from 12)

Examples

Example 1: Token-protected orders API

User request: "Add an orders API to my Laravel app with login tokens, nothing for the browser."

php artisan install:api && php artisan make:model Order -mfcr --api
# edit the migration: user_id, reference, status, total_cents
php artisan migrate
php artisan test --compact

Add HasApiTokens to User, paste the token route and the auth:sanctum group from above into routes/api.php. Then curl -H "Authorization: Bearer 1|k9Xa..." -H "Accept: application/json" http://localhost:8000/api/orders returns a paginated JSON object; without the token it returns 401 {"message":"Unauthenticated."}.

Example 2: Move PDF invoice generation off the request

User request: "Generating the invoice PDF takes 6 seconds. Make the order endpoint return immediately."

php artisan make:job GenerateInvoicePdf

Implement ShouldQueue with $tries = 3, call GenerateInvoicePdf::dispatch($order)->onQueue('invoices'); in the controller and run php artisan queue:work --queue=invoices,default. The endpoint now answers in milliseconds, and failures are retried and listed by php artisan queue:failed.

Guidelines

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.