Routing
4 snippetsDefine 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 snippetsModels, 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 snippetsTemplate 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.
Migrations
3 snippetsDatabase 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 snippetsCommon 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 snippetsFluent 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));