PHP & Laravel
Build web applications with PHP and Laravel - Eloquent ORM, routing, Blade templates, queues, and modern PHP 8.x features.
skill: PHP & Laravel category: Backend tags: [PHP, Laravel, Eloquent, Blade, Composer] readingTime: 5
Laravel is the most productive PHP framework - it provides an elegant abstraction over PHP's rough edges. Modern PHP (8.0+) is a capable language with named arguments, match expressions, fibers, and attributes. Laravel's ecosystem (Forge, Vapor, Nova, Sanctum) covers deployment, serverless, admin panels, and API authentication.
Workflow
- Use Eloquent ORMEloquent's Active Record pattern maps database tables to model classes. Relationships (hasMany, belongsTo) make joins readable and automatic.
- Define routes and middlewareLaravel's routing is expressive and readable. Middleware handles cross-cutting concerns like authentication, rate limiting, and CORS.
- Queue background jobsLaravel Queues process time-consuming tasks (emails, file processing, webhooks) asynchronously, using Redis or database as queue driver.
- Use modern PHP 8.x featuresPHP 8.0+ added constructor promotion, named arguments, match expressions, and enums - reducing boilerplate and improving readability.
- Test with PestPest is Laravel's preferred testing framework - a cleaner, more expressive API than PHPUnit with better output and Laravel-specific helpers.
Output Format
Laravel: Eloquent with() eager loading + scopes, apiResource routes + FormRequest validation, ShouldQueue jobs with backoff, PHP 8 enums + match + constructor promotion, and Pest feature tests.
Eloquent: Order::with(["user","items"])->pending()->paginate(20)
Route: Route::middleware(["auth:sanctum"])->apiResource("orders", Controller)
Job: class SendEmail implements ShouldQueue { handle(Mailer $m) }
PHP 8: enum Status: string { case Pending = "pending" }
Pest: it("creates order") { actingAs($user)->postJson()->assertStatus(201) }Examples
Input
Output
Edge Cases & Gotchas
- Mass assignment protection - $fillable or $guarded must be set on every model. Without $fillable, using Model::create($request->all()) exposes every column to user-controlled mass assignment - a security vulnerability.
- N+1 in blade templates - Blade loops that access relations without eager loading create N+1. Use Laravel Debugbar in development to catch N+1 queries during development.
- PHP session locking - PHP file sessions lock on concurrent requests from the same user - AJAX requests queue behind each other. Use Redis session driver (SESSION_DRIVER=redis) to avoid this.
- Queue job idempotency - Laravel's at-least-once delivery means jobs may run twice. Use database locks or check before acting: if (Order::find($id)?->status === "pending") { process(); }
When to Break These Rules
- Use TypeScript/Node.js instead of PHP when your team's expertise is JavaScript-first - the productivity gain from language familiarity outweighs Laravel's framework benefits for JavaScript developers.
- Use Laravel Livewire instead of Vue/React for server-heavy PHP applications where you want interactive UIs without a full JavaScript framework - similar to Phoenix LiveView.
Reference Files
https://laravel.com/docs/https://pestphp.com/