且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel迁移-在表中添加检查约束

更新时间:2022-06-13 07:53:34

Blueprint类不支持添加约束(至少从Laravel 5.3起),但是 可以向其中添加约束通过使用数据库语句直接从迁移中创建表.

Adding constraints is not supported by the Blueprint class (at least as of Laravel 5.3), however it is possible to add constraints to your tables directly from your migrations, by using database statements.

在您的迁移文件中,

public function up ()
{
    Schema::create('payroll', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('position_id');
        $table->decimal('salary',9,2);
    });

    // Add the constraint
    DB::statement('ALTER TABLE payroll ADD CONSTRAINT chk_salary_amount CHECK (salary < 150000.00);');
}