Eloquent Models and Migrations
Generate models and database migrations with Artisan commands.
Eloquent Models and Migrations is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is Eloquent?
Eloquent is Laravel' ActiveRecord ORM. Each database table has a corresponding Model class that represents a row and provides query methods.
Generating a Model
Use Artisan to create a model. Add -m to also generate a migration file.
$ php artisan make:model Post -m
# Creates:
# app/Models/Post.php
# database/migrations/xxxx_create_posts_table.phpThe Model Class
A minimal Eloquent model just needs to extend Model. Eloquent infers the table name from the class name (plural snake_case).
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = ["title", "body", "user_id"];
}Migration File
The migration defines the table schema. Run php artisan migrate to apply it.
<?php
public function up(): void
{
Schema::create("posts", function (Blueprint $table) {
$table->id();
$table->string("title");
$table->text("body");
$table->foreignId("user_id")->constrained()->cascadeOnDelete();
$table->timestamps();
});
}Running Migrations
Apply pending migrations to the database.
$ php artisan migrate
# Rollback:
$ php artisan migrate:rollback
# Fresh (drop all and re-migrate):
$ php artisan migrate:fresh$fillable vs $guarded
$fillable whitelists columns for mass-assignment. $guarded blacklists columns. Never set $guarded = [] in production without careful thought.
<?php
// Whitelist approach (recommended):
protected $fillable = ["title", "body"];
// Blacklist approach:
protected $guarded = ["id", "created_at"];Casts
The $casts property automatically converts column values to PHP types.
<?php
protected $casts = [
"is_published" => "boolean",
"metadata" => "array", // JSON column
"published_at" => "datetime",
];Hidden and Visible
The $hidden property excludes columns from JSON/array serialization — useful for passwords and tokens.
<?php
protected $hidden = ["password", "remember_token"];Custom Table Name
Override the default table name with the $table property.
<?php
protected $table = "blog_posts"; // instead of "posts"Soft Deletes
Add the SoftDeletes trait and a deleted_at column. Records are not actually deleted — they are flagged with a timestamp and excluded from normal queries.
<?php
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model {
use SoftDeletes;
}Timestamps
By default Eloquent manages created_at and updated_at columns. Set $timestamps = false to disable.
Summary
Eloquent models map to database tables. Use Artisan to generate models + migrations together. Define $fillable for mass-assignment safety. Use $casts for automatic type conversion.
Quick Check
Which Artisan flag generates a migration alongside a model?
Frequently asked questions
Is the “Eloquent Models and Migrations” lesson free?
Yes — the full text of “Eloquent Models and Migrations” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Eloquent Models and Migrations”?
Generate models and database migrations with Artisan commands. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Eloquent Models and Migrations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Eloquent Models and Migrations
- CRUD with Eloquent
- Eloquent Relationships
- Eager Loading and Query Scopes