Why this lesson matters

Understand migrations and models and apply it in a small Laravel feature. Migrations define database evolution, Eloquent models represent records and relationships, and queries should preserve the integrity constraints the schema is designed to enforce.

How to reason about it

  • For Migrations and Models, the outcome to verify is: Put important integrity constraints in the database, not only in UI validation.
  • In Migrations and Models, keep this failure controlled: Relying only on application validation while omitting database keys, foreign keys or appropriate column types leaves integrity vulnerable to other writers.
  • Migrations and Models practice target: Create a parent and child table with a foreign key, model the relation in Eloquent, insert data and query it through the relationship.

Practical walkthrough

In the Migrations and Models walkthrough: Put important integrity constraints in the database, not only in UI validation.

database/migrations/create_orders_table.phpphp
Schema::create('orders', function (Blueprint $table) {
    $table->id();
    $table->foreignId('user_id')->constrained()->cascadeOnDelete();
    $table->decimal('total', 12, 2);
    $table->timestamps();
});

Practice it yourself

Practice

Migrations and Models exercise

Create a parent and child table with a foreign key, model the relation in Eloquent, insert data and query it through the relationship.

  • Record the expected result before execution
  • Test one valid path and one lesson-specific failure path
  • Explain in two lines which boundary owns the decision

Summary