DEV Community

trungpv
trungpv

Posted on

Laravel: Use Increment IDs , UUIDs, ULIDs as primary key or optional field for the Eloquent model

I think out there are too many posts that talk about this but If you see this post. Thank you for reading. I just want to share my journey and experience with it 🧐.

In the beginning, I have no idea about the pros and cons of the UUIDs and ULIDs. I know it is an idea that helps us hide the increment IDs from the end-user who can not guest it 🐣.

So, I finish all the steps on the tutorial through the internet. Or maybe my English is not good enough to understand deeply the idea 🀯.

After I use it for a few projects and practice it. I will tell you about it and I want to challenge myself to write a blog like
a habit.

Use Increment IDs , UUIDs, ULIDs

I think for the beginner you should not use it except that you must need it in the project for some reasons like hiding increment IDs, distributing database, big data...

Because I saw that it makes your DB bigger for the space, slower query, and inconvenience when we use other libraries.

πŸ‘‰ The answer is which one you feel happy when you work with and keep it simple that use Increment IDs

Use UUIDs, ULIDs as Primary key or Optional field

I will write down my experience and my suggestion when I use it then you guys can decide πŸ€“ on your project.

In the beginning, I use it as the primary key of the table. Everything is good except I need to custom the migration of the lib when I use third-party libs. But I think we will deal with some issues like slow query because I used to query chunk the table so we need to keep one field in order like the increment IDs. The disk space will be bigger compared with the Increment IDs.

Now, I think I will use the UUIDs, ULIDs like the optional field. Because we can keep the default structure of the eloquent model and we can have another unique field to adapt to the small requirement of the project.

In the end, We should use the thing in the project scope.

Top comments (0)