i'm trying to generate a string for user's api_token in my database using str_random(60)
and unique()
function but looks like i'm doing it wrong. When i seed using db:seed
two dummy users they should have different api_token(s). Here's my migration file
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('api_token')->default(str_random(60))->unique();
$table->string('password');
$table->string('remember_token')->nullable();
$table->timestamps();
If i remove the unique()
function the users will have same api_token and if i put back that unique()
, i will have an error because they will have same api_token. Can someone help me for this unique api_token case, please?
You shouldn't use default()
with unique()
. Because default()
is a value which is set to a row when there isn't any value put in this column. So you shouldn't use both together.
Change it to:
$table->string('api_token')->unique();
And
You must set the api_token in seeder, not in migration.