0

I have made a custom primary key in Laravel. On my model

protected $primaryKey = ['profileId'];

public $incrementing = false;

Migration = $table->integer('profileId')->unsigned();

$table->primary('profileId');  

Is there use of making a custom primary key unsigned(). ?

Maxim Krizhanovsky
  • 25,260
  • 5
  • 51
  • 86

4 Answers4

1

you use unsigned() to have only positive values, since AUTO_INCREMENT starts at zero by default and increments in the positive direction, it's more convenient to utilize the positive values than the negative value.On your mogration file add:

$table->primary('profileId')->unsigned();

In case you want a string as primary key then;

$table->string('column', 30)->primary();   

and

public $incrementing = false;
Leo
  • 6,808
  • 5
  • 20
  • 44
0

In your migration when you are creating the table you can indicate the primary key with primary():

Schema::create('table', function (Blueprint $table) {
   $table->primary('profileId');  
});

If your primary key is a string you need to add to your model:

public $incrementing = false;
Troyer
  • 6,347
  • 2
  • 33
  • 60
  • so no need to make unsigned() ? and if data type of primary key is string or some other data type.? – Nitin Srivastava Aug 23 '17 at 12:54
  • Unsigned values means that the value will not have `+` or `-`signs, you have a lot of information. Like: https://stackoverflow.com/questions/13553707/what-does-signed-and-unsigned-values-mean – Troyer Aug 23 '17 at 12:56
0
class UserVerification extends Model
{
protected $primaryKey = 'your_key_name'; // or null

public $incrementing = false;
}
Kuldeep Mishra
  • 3,395
  • 1
  • 18
  • 25
  • 1
    Code-only answers are discouraged; would you mind explaining what you're doing so future readers can more easily understand? – Johnny Rockex Aug 23 '17 at 15:56
0

As per the documentation, You can try below code if your primary key is a string:

$table->tinyInteger('profileId');
$table->primary('profileId');

And if you want it as auto increment you have to do something like this

$table->tinyInteger('profileId')->unsigned()->autoIncrement()‌​;
AddWeb Solution Pvt Ltd
  • 20,310
  • 5
  • 23
  • 55