Courses

Structuring Databases in Laravel 11

UUID or ULID to Hide the Main ID

Summary of this lesson:
- Implementing UUID in Laravel
- Using ULID for sortable identifiers
- Managing route model binding
- Choosing between ID types

In this lesson, we will discuss UUID and ULID and how to use them in Laravel.

UUID stands for Universally Unique Identifier, and ULID stands for Universally Unique Lexicographically sortable Identifier. These are not Laravel or PHP terms, their application is broader. It's a long sequence of digits and letters to encode a more extended identifier instead of an ID field.

Now, why would you need that?


UUID

The most typical example is when you want to show a user's profile but hide the ID from the URL. For that, you may want to use a UUID.

How to use UUIDs within Laravel? First, we must create a UUID DB column.

database/migrations/xxx_add_uuid_to_users_table.php:

Schema::table('users', function (Blueprint $table) {
$table->uuid();
});

In my opinion, you should add your UUID in addition to the primary ID, not instead of ID. It would help if...

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