laravel自定义主键属性

时间:2017-08-23 12:41:29

标签: database laravel

我在Laravel中制作了一个自定义主键。在我的模特

protected $primaryKey = ['profileId'];

public $incrementing = false;

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

$table->primary('profileId');  

是否可以使用自定义主键unsigned()。 ?

4 个答案:

答案 0 :(得分:1)

你使用unsigned()只有正值,因为AUTO_INCREMENT默认从零开始并且在正方向递增,使用正值比使用负值更方便。在你的mogration上文件添加:

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

如果您想要一个字符串作为主键,那么;

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

public $incrementing = false;

答案 1 :(得分:0)

在创建表格时的迁移中,您可以使用primary()指示主键:

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

如果您的主键是一个字符串,则需要添加到模型中:

public $incrementing = false;

答案 2 :(得分:0)

class UserVerification extends Model
{
protected $primaryKey = 'your_key_name'; // or null

public $incrementing = false;
}

答案 3 :(得分:0)

根据documentation,如果您的主键是字符串,则可以尝试以下代码:

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

如果你想要它作为自动增量你必须做这样的事情

$table->tinyInteger('profileId')->unsigned()->autoIncrement()‌​;