📊 Capítulo 05: Migrations & Schema Builder
Especialização Backend com PHP & Laravel
🗺️ O Fluxo de Versionamento de Banco com Migrations
flowchart TD
ARTISAN["php artisan make:migration create_pedidos_table"]
--> SCHEMA["Schema::create('pedidos', function (Blueprint $table) { ... })"]
--> UP["$table->id(); $table->foreignId('usuario_id')->constrained();"]
--> MIGRATE["php artisan migrate"]
--> DB_VERSION["Tabela migrations registra batch de execução no PostgreSQL / MySQL"]
--> ROLLBACK["php artisan migrate:rollback (Executa método down())"]
style ARTISAN fill:#ede7f6,stroke:#512da8,stroke-width:2px
style SCHEMA fill:#fff3e0,stroke:#ff9800,stroke-width:2px
style UP fill:#e8f5e9,stroke:#4caf50,stroke-width:1.5px
style MIGRATE fill:#e1f5fe,stroke:#03a9f4,stroke-width:2px
style DB_VERSION fill:#c8e6c9,stroke:#2e7d32,stroke-width:1.5px
style ROLLBACK fill:#ffebee,stroke:#d32f2f,stroke-width:1.5px
🎯 Slide 1: Sintaxe Moderna do Schema Builder
Schema::create('produtos', function (Blueprint $table) {
$table->id();
$table->string('nome', 120)->index();
$table->decimal('preco', 10, 2);
$table->foreignId('categoria_id')->constrained('categorias')->cascadeOnDelete();
$table->json('atributos')->nullable();
$table->timestamps();
$table->softDeletes();
});
🎯 Slide 2: Anonymous Migrations no Laravel
- As migrações modernas do Laravel retornam uma classe anônima (
return new class extends Migration), eliminando conflitos de nomenclatura de classes em projetos grandes!
🎯 Slide 3: Boas Práticas de Índices e Chaves Estrangeiras
-
foreignId()->constrained(): Cria a coluna unsigned bigint e a constraint de chave estrangeira em 1 única linha. - Índices Compostos:
$table->index(['status', 'created_at'])para acelerar queries com múltiplos filtros.
Slide 1 de 1
Atalhos: ← → Navegar • Espaço Avançar • F Tela Cheia • Home Início • End Fim