Laravel Pro Tips

Laravel Pro Tips

Share this post

Laravel Pro Tips
Laravel Pro Tips
UUID in Laravel: A Simple Guide

UUID in Laravel: A Simple Guide

Looking to upgrade from basic database auto-increment IDs? Consider UUID! In this easy-to-follow guide, let's dive into understanding UUID columns in Laravel and explore the various tools and methods.

Laravel Pro Tips's avatar
Laravel Pro Tips
Oct 28, 2023
∙ Paid
1

Share this post

Laravel Pro Tips
Laravel Pro Tips
UUID in Laravel: A Simple Guide
Share

black laptop computer turned on displaying blue screen
Photo by Mohammad Rahmani on Unsplash

Using UUID as Your Primary Key in Laravel

Laravel has made using UUIDs as primary keys a breeze since version v9.30. Here's a quick breakdown to get you started:

Swap out your usual ID creation with:

// Don't use: $table->id();
$table->uuid('id')->primary();

Add the Laravel-provided UUID trait:

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;

class Article extends Model {
    use HasUuids;
}

Save your record with Eloquent, and a UUID will be auto-generated:

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2025 Laravel Pro Tips
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share