DM
Technical reference

Laravel Cheatsheet

Productive PHP web application framework

Must Know

bash

Common Commands

Artisan generates framework-aware boilerplate.

php artisan serve
php artisan make:model Post -mcr
php artisan migrate
php artisan route:list
php

Route and Controller

Route model binding resolves the model automatically.

Route::get('/posts/{post}', [PostController::class, 'show']);

public function show(Post $post) {
    return new PostResource($post);
}

Important Patterns

php

Validate Requests

Never trust request input.

$validated = $request->validate([
    'title' => ['required', 'string', 'max:255'],
    'email' => ['required', 'email'],
]);
php

Eloquent Relationships

Eager load relationships to avoid N+1 queries.

class Post extends Model {
    public function author() { return $this->belongsTo(User::class); }
}

$posts = Post::with('author')->paginate(20);

Useful Recipes

php

Database Transaction

Group dependent writes atomically.

DB::transaction(function () use ($data) {
    $order = Order::create($data);
    $order->items()->createMany($data['items']);
});
bash

Queue a Job

Move slow, retryable work out of requests.

ProcessUpload::dispatch($upload)->onQueue('media');

php artisan queue:work --tries=3

Pitfalls & Production

php

Mass Assignment

Do not pass unfiltered request data to models.

protected $fillable = ['name', 'email'];

User::create($request->only(['name', 'email']));
bash

Production Cache

Do not call env() outside config files when config is cached.

php artisan config:cache
php artisan route:cache
php artisan view:cache