uniqid() generating same string on each create record.

Published 5 months ago by jlsodai

I am quite new to Laravel and need your help. I want to create a unique string for the uniq field... But laravel is giving me the same value over and over again. Any help on why this is happening or a better way to generate a unique but not too long id (eg. 5a5971196db9e) will be much appreciated.

Schema::create('schools', function (Blueprint $table) {
            $table->increments('id');
            $table->string('uniq')->default(uniqid());
});
Best Answer (As Selected By jlsodai)
ctorresr

You are defaulting the value for the uniqid. I recommend you read again https://laravel.com/docs/5.5/migrations to understand migrations. As per a better solution I would recommend https://packagist.org/packages/ramsey/uuid .

ctorresr

You are defaulting the value for the uniqid. I recommend you read again https://laravel.com/docs/5.5/migrations to understand migrations. As per a better solution I would recommend https://packagist.org/packages/ramsey/uuid .

Snapey
Snapey
5 months ago (894,865 XP)

It should be working but I don't understand the code.

You are creating are creating a table called 'schools' and adding two columns to it.

How can this ever produce more than one result?

Please sign in or create an account to participate in this conversation.