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
  • 26,265
  • 5
  • 59
  • 89

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
  • 7,274
  • 5
  • 26
  • 48
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,765
  • 3
  • 34
  • 62
  • 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,846
  • 1
  • 21
  • 26
  • 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
  • 21,025
  • 5
  • 26
  • 57