简体   繁体   English

如何在laravel迁移期间设置初始值?

[英]How to set initial value during laravel migration?

I'm using Laravel migration to add new column location in the table books我正在使用 Laravel 迁移在表格books添加新的列location

<?php

class AddNewColumn extends Migration
{
    public function up()
    {
        Schema::table('books', function (Blueprint $table) {
            $table->text('location')->nullable()->after('description');
        });
    }
}

I want to add some predefined location value for each record of "book".我想为“书”的每条记录添加一些预定义的location值。 I get it from another table called "locations".我从另一个名为“locations”的表中得到它。 So for each book location is different and it's not a default value.所以对于每本书的位置都是不同的,它不是默认值。 Is there some way set initial value for the column in the migration?是否有某种方式为迁移中的列设置初始值?

I hope the link below will be useful for you.我希望下面的链接对您有用。

edit: if the default value is supposed to be null, make it nullable instead.编辑:如果默认值应该为空,请改为可空。

https://laravel.com/docs/8.x/migrations#default-expressions https://laravel.com/docs/8.x/migrations#default-expressions

<?php

    class AddNewColumn extends Migration
    {
        public function up()
        {
            Schema::table('books', function (Blueprint $table) {
                $table->text('location')->after('description')->default('1');
            });
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM