AI Generate Laravel docs instantly

Laravel Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Routing

4 snippets

Define application routes

Basic Routes

Route::get('/posts', [PostController::class, 'index']);
Route::post('/posts', [PostController::class, 'store']);
Route::put('/posts/{post}', [PostController::class, 'update']);
Route::delete('/posts/{post}', [PostController::class, 'destroy']);

Resource Route

Route::resource('posts', PostController::class);
Route::apiResource('posts', PostController::class);
// Generates: index, create, store, show, edit, update, destroy

Middleware & Groups

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/dashboard', DashboardController::class);
});
Route::prefix('api/v1')->group(function () {
    Route::apiResource('posts', PostController::class);
});

Route Model Binding

// Implicit binding
Route::get('/posts/{post}', function (Post $post) {
    return $post;
});
// Custom key
Route::get('/posts/{post:slug}', function (Post $post) {
    return $post;
});

Eloquent ORM

4 snippets

Models, relationships, scopes

Relationships

// In Post model
public function author(): BelongsTo {
    return $this->belongsTo(User::class);
}
public function tags(): BelongsToMany {
    return $this->belongsToMany(Tag::class);
}
public function comments(): HasMany {
    return $this->hasMany(Comment::class);
}

Scopes

// In model
public function scopePublished($query) {
    return $query->where('published', true);
}
public function scopeRecent($query, $days = 7) {
    return $query->where('created_at', '>=', now()->subDays($days));
}
// Usage
Post::published()->recent()->get();

Query Builder

Post::where('published', true)
    ->whereHas('comments', fn ($q) => $q->where('approved', true))
    ->withCount('comments')
    ->orderByDesc('created_at')
    ->paginate(15);

Accessors & Casts

// In model
protected function name(): Attribute {
    return Attribute::make(
        get: fn ($value) => ucfirst($value),
    );
}
protected $casts = [
    'options' => 'array',
    'published_at' => 'datetime',
];

Blade Templates

3 snippets

Template engine syntax

Output & Control

{{ $variable }}
{!! $rawHtml !!}

@if($posts->count())
  @foreach($posts as $post)
    <li>{{ $post->title }}</li>
  @endforeach
@else
  <p>No posts.</p>
@endif

Components

{{-- resources/views/components/alert.blade.php --}}
<div class="alert alert-{{ $type }}">
    {{ $slot }}
</div>

{{-- Usage --}}
<x-alert type="success">Saved!</x-alert>

Layouts

{{-- layout.blade.php --}}
<html><body>@yield('content')</body></html>

{{-- page.blade.php --}}
@extends('layout')
@section('content')
  <h1>Hello</h1>
@endsection

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Migrations

3 snippets

Database schema management

Create Table

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->foreignId('user_id')->constrained()->cascadeOnDelete();
    $table->string('title');
    $table->text('body');
    $table->boolean('published')->default(false);
    $table->timestamps();
    $table->softDeletes();
});

Modify Table

Schema::table('posts', function (Blueprint $table) {
    $table->string('slug')->unique()->after('title');
    $table->index(['user_id', 'created_at']);
    $table->fullText('body');
});

Commands

php artisan make:migration create_posts_table
php artisan migrate
php artisan migrate:rollback
php artisan migrate:fresh --seed

Artisan Commands

3 snippets

Common artisan commands

Generate

php artisan make:model Post -mfc
# Creates model, migration, factory, controller
php artisan make:request StorePostRequest
php artisan make:job ProcessPodcast
php artisan make:livewire Counter

Tinker

php artisan tinker
>>> Post::factory()->count(10)->create();
>>> User::where('email', 'a@b.com')->first();
>>> Cache::flush();

Queue & Cache

php artisan queue:work --tries=3
php artisan queue:retry all
php artisan cache:clear
php artisan config:cache
php artisan route:cache

Collections

3 snippets

Fluent array manipulation

Transform

collect([1, 2, 3])->map(fn ($n) => $n * 2);  // [2, 4, 6]
collect($users)->pluck('email');  // ['a@b.com', ...]
collect($users)->keyBy('id');

Filter & Sort

collect($items)->filter(fn ($i) => $i->active);
collect($items)->reject(fn ($i) => $i->archived);
collect($items)->sortByDesc('created_at');

Aggregate

collect($orders)->sum('total');
collect($scores)->avg();
collect($users)->groupBy('role');
collect($items)->chunk(100)->each(fn ($chunk) => process($chunk));

More Cheat Sheets

FAQ

Frequently asked questions

What is a Laravel cheat sheet?

A Laravel cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Laravel. It helps developers quickly look up syntax without searching through documentation.

How do I learn Laravel quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Laravel concepts?

Key Laravel concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Laravel code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Laravel code using AI.

Related resources

Stop memorizing. Start shipping.

Generate Laravel Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week