Courses

Structuring Databases in Laravel 11

Enum DB Columns, PHP Enums and Foreign Keys

Summary of this lesson:
- Using native PHP enums
- Implementing database enum columns
- Managing enum validation
- Comparing performance between approaches

In this lesson, we will discuss different kinds of Enums. There are two ways to use Enums: as a database column or with PHP native Enums.


Option 1: Database Column

First, let's look at the database enum column. You can create it by defining the enum() column in the migrations and providing the values as a second column in an array.

Schema::create('tickets', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->enum('status', ['open', 'closed'])->default('open');
$table->timestamps();
});

But with database fields come some issues. First, where should these values be saved in the code? For example, when creating or editing a ticket, a user might need to be able to select a status. You may need to filter by ticket status somewhere.

One way to deal with this issue could be to define statuses as...

The full lesson is only for Premium Members.
Want to access all 18 lessons of this course? (81 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord