我是否需要将学生ID定义为主键并更改laravel中eloquent orm中的默认ID?

时间:2016-01-23 12:29:27

标签: mysql laravel eloquent

我有一个学校的学生数据库。比方说,例如,一名学生的学生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();
    });

1 个答案:

答案 0 :(得分:1)

我会保留自动增加ID,并为您自己的已编入索引且唯一的学生ID 098-123-123使用其他列。然后创建一个范围,以便能够通过此学生ID快速搜索。

public function scopeFindByStudentId($query, $id)
{
    $query->where('student_id', '=', $id);
}
相关问题