search

Home  >  Q&A  >  body text

Best way to manage public and internal identifiers in Laravel REST API using UUID and slug columns

I have a table that contains classic auto-increment IDs and company names. To avoid exposing the business ID to the client, I want to use UUID. So far, so good. The only problem is that for calling it from a URL, it's better to use a more user-friendly format, such as "api/businesses/my-business" instead of "api/businesses/10b940f2-5f8c-42ac-9c35-b6d0de45995b". So if I add a "slug" column to a table for GET requests while using UUID for data updates, would this be considered a best practice?

In my case I need to create a record in the quote table, so the PATCH will be:

PATCH /api/quotes/4dc93692-0ad9-4131-94fe-b4afec88d037

{
    "business_uuid": "10b940f2-5f8c-42ac-9c35-b6d0de45995b",
    "object": "My quote object",
    "another_column": "Hello",
}

P粉360266095P粉360266095246 days ago364

reply all(1)I'll reply

  • P粉262073176

    P粉2620731762024-03-29 09:51:26

    If your database table structure contains id, uuid, slug, please consider the following:

    1. Only used inside the backend id.
    2. Use uuid when handling resources using the REST API (CRUD).
    3. Use slug when you want to handle resources where it is easier for humans to read/recognize/understand the data. Don't forget that slug must be unique. But for basic CRUD operations between services, I still recommend continuing to use uuid.

    I also recommend checking out the Laravel documentation on Laravel Resources that can help you prepare data for the API, and the slugify helper function that handles one of your data fields.

    reply
    0
  • Cancelreply