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粉2620731762024-03-29 09:51:26
If your database table structure contains id
, uuid
, slug
, please consider the following:
id
. uuid
when handling resources using the REST API (CRUD). 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.