LindaA's avatar

Create unique value in laravel?

Hi people, I'm new in laravel, I have a question related to database, which function should I use to create a unique string in database?

0 likes
3 replies
InaniELHoussain's avatar

if you are using Laravel's validation make sure to use the rule unique for example

'name' => 'required|unique:table, field'
tykus's avatar

I don't know what exactly you are looking for here, so make of the following what you want:

  • MySQL has a UUID() function which will generate a unique string
  • PHP has a uniqid() function which will generate a unique string

You can enforce the uniqueness in the database by specifying a unique index on the relevant column, and/or in code using the unique validation rule.

Chrizzmeister's avatar

Depends on what you mean. If you mean checking for a unique value in the controller (so when you validate a form input for example) @InaniELHoussain answer is correct. But if you mean on the migration yourself, you can just append ->unique() to the migration . For example: $table->string('name')->unique();

You should do it both btw.

2 likes

Please or to participate in this conversation.