Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chern123's avatar

Generating unique strings for a URL

I have seen many different implementations of this for different languages/frameworks, but I am wondering if Laravel might not have some useful helper methods that may make this an easier task.

In short, I would like to generate a random, unique string for each instance of a resource in my project. Let's say the resource is a 'test'. Upon test creation, I would like to assign it said string, with the end goal of being able to allow people to access a given resource based on this generated, unique value:

test.app/af3123!
test.app/gxcr_2!

I don't really care if they are all the same length or not, though I would prefer not to have something longer than say 6-8 characters.

At first glance I figured just to generate random combinations of strings, and test to see if such a combination has been used in the database yet. However, I imagine that might get problematic if the database becomes sufficiently large.

Any ideas?

0 likes
2 replies
willvincent's avatar
Level 54

Sounds like what you want is the Hash IDs package.

You don't necessarily need it, but there's also another package that provides a laravel wrapper around it you could use if you prefer using facades..

1 like

Please or to participate in this conversation.