我有这个学校的学生数据库。例如,假设一名学生的学生ID为098-123-123。我希望那个学生号成为主键。是否建议使用字符串而不是增量?我是否需要更改表的结构并更改laravel中的默认增量('id')?最佳做法是什么?
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('privillege');
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
最佳答案
我会保留自动增量ID,并使用另一列作为您自己的学生ID 098-123-123
的唯一索引。然后创建一个范围,以便能够通过此学生ID快速搜索。
public function scopeFindByStudentId($query, $id)
{
$query->where('student_id', '=', $id);
}
关于mysql - 我是否需要将学生ID定义为主键,并在laravel中 Eloquent orm中更改默认ID?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34963468/