php artisan make:model Lesson -m creates a model and a migration. A migration describes a table; php artisan migrate creates it in the database set in your .env file.
<?php
// database/migrations/xxxx_create_lessons_table.php
Schema::create('lessons', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('summary');
$table->boolean('published')->default(false);
$table->timestamps(); // created_at and updated_at
});The model
The Lesson model is connected to the lessons table by name. $fillable lists the columns that create may fill, which protects the others.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Lesson extends Model
{
protected $fillable = ['title', 'summary', 'published'];
}Queries
<?php
$all = Lesson::all();
$published = Lesson::where('published', true)->orderBy('title')->get();
$lesson = Lesson::findOrFail($id); // a 404 page if there is no such lesson
Lesson::create(['title' => 'Eloquent', 'summary' => 'Talk to the database with models.']);Each query builds the same SQL you learned in “SQL SELECT Basics”; Eloquent writes it for you.
Code examples
<?php
$titles = Lesson::where('published', true)
->orderBy('title')
->pluck('title');
// Runs: select title from lessons where published = 1 order by title ascExpected output
A list of published lesson titles in A–Z order, e.g. ["Blade", "Eloquent", "Routes"]
pluck returns just one column instead of whole Lesson objects.
Common mistakes
Common mistake: MassAssignmentException when calling create
Add the column names to the model's $fillable array.
Common mistake: “Base table or view not found”
The table doesn't exist yet. Run php artisan migrate after creating or changing a migration.
Check your understanding
3 quick questions about this lesson.