Chapter 2.2: Creating the Planet Model
Time to learn: 45 minutes
1. The Model in Laravel: A Blueprint for a Celestial Object
In the MVC (Model-View-Controller) architecture, the model is:
- 📦 A container for data (planet parameters)
- 🔌 An interface for working with the database
- 🛡️ The center of the application's business logic
💡 Space Analogy: The
Planet
model = A planet's blueprint in the Mission Control Center's computer. With it, you can:
- Create a new planet in the catalog (
INSERT
)- Get data about Mars (
SELECT
)- Update information about the atmosphere (
UPDATE
)
2. Creating a Model and Migration
Laravel uses the Artisan CLI—your project's "control panel."
Step 1: Generating the model with a migration
What was created:
app/
└── Models/
└── Planet.php ← Model
database/
└── migrations/
└── 2025_08_04_000000_create_planets_table.php ← Migration
Artisan Flags:
-m
→ Create a migration-c
→ Create a controller-r
→ Resource controller
💡 Pro Tip: These flags can be combined for maximum efficiency. The command
php artisan make:model Planet -mcr
will create a model, a migration, and a resource controller to manage that model all at once. This saves a lot of time.
3. Configuring the Planet Model
Open app/Models/Planet.php
:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Planet extends Model
{
// Table name (default: 'planets')
// Laravel automatically assumes the table name is the plural, snake_case version of the model name (Planet -> planets).
// Therefore, this property isn't strictly necessary here, but it's useful for clarity.
protected $table = 'planets';
// The attributes that are mass assignable.
protected $fillable = [
'name',
'description',
'size_km',
'solar_system',
'image_url'
];
// The attributes that should be cast.
protected $casts = [
'size_km' => 'integer'
];
}
Property Explanation:
Property | Meaning | Example Data |
---|---|---|
$table |
Table name in the DB | planets |
$fillable |
Fields for mass assignment | name , size_km |
$casts |
Automatic type casting | size_km: integer |
4. Designing the Planets Table
Before editing the migration, let's define the "planet" structure:
Field | Data Type | Description | Example Value |
---|---|---|---|
id |
BIGINT (PK) | Unique ID | 1 |
name |
VARCHAR(255) | Planet's name | "Mars" |
description |
TEXT | Description | "The Red Planet..." |
size_km |
INTEGER | Diameter in km | 6779 |
solar_system |
VARCHAR(100) | Solar system | "Solar System" |
image_url |
VARCHAR(2048) | Photo URL (can be null) | "https://..." |
created_at |
TIMESTAMP | Creation date | 2025-08-04 12:00:00 |
updated_at |
TIMESTAMP | Update date | 2025-08-05 09:30:00 |
5. Editing the Migration
Open the migration file in database/migrations/..._create_planets_table.php
:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePlanetsTable extends Migration
{
public function up()
{
Schema::create('planets', function (Blueprint $table) {
$table->id(); // BIGINT auto-increment
$table->string('name')->unique();
$table->text('description');
$table->integer('size_km');
$table->string('solar_system', 100);
$table->string('image_url', 2048)->nullable();
$table->timestamps(); // created_at + updated_at
});
}
public function down()
{
Schema::dropIfExists('planets');
}
}
Key Points:
->unique()
→ Ensures names are unique->nullable()
→ The field can be emptytimestamps()
→ Automatic date management
6. Running the Migration (Preview)
Although we'll run this in Chapter 2.3, let's see what it looks like:
Expected Output:
Migrating: 2025_08_04_000000_create_planets_table
Migrated: 2025_08_04_000000_create_planets_table (32.15ms)
Checking in pgAdmin 4:
- Open the
space_api
database → Schemas → Tables - The
planets
table should appear with your fields
7. Alternative Approach: Code Generators
To speed up development, you can use packages:
- Laravel Blueprint - creates models/migrations from a YAML definition
- InfyOm Laravel Generator - a generator based on a DB schema
Blueprint Example:
models:
Planet:
name: string:255
description: text
size_km: integer
solar_system: string:100
image_url: string:2048? # The '?' character means the field is nullable
Review Quiz
🚀 Chapter Summary: You've created a "digital twin" of a planet! Your project now has:
- 🪐 A
Planet
model with business logic - 📊 A migration for the
planets
table in PostgreSQL - 🛠️ Configured model properties (
fillable
,casts
)
Get the database ready! In the next chapter, we will run the migration and populate the universe with its first planets.
📌 Checkpoint:
Make sure the
Planet.php
and..._create_planets_table.php
files are created in the correct directories.⚠️ If Artisan gives an error:
- Check that you are in the project folder
- Make sure dependencies are installed (
composer install
)- For Windows: add PHP and Composer to your PATH