简体   繁体   English

Laravel - 在记录更新时设置列的默认值

[英]Laravel - Set default value of column on record update

I have this migration:我有这个迁移:

class CreatePendingCredits extends Migration
{
    public function up()
    {
        Schema::create('pending_credits', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique()->default(DB::raw('UUID()'));
            $table->foreignIdFor(App\Models\User::class, 'user_id')->references('id')->on('users');
            $table->double('amount');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('pending_credits');
    }
}

When I'm creating a new record for pending_credits , I don't set uuid attribute, so database will generate a new uuid (uses the default value).当我为pending_credits创建新记录时,我没有设置uuid属性,因此数据库将生成一个新的 uuid(使用默认值)。

Let's say for some reason I need to update the uuid of one record just like when I'm creating a new record and database generates the uuid.假设出于某种原因,我需要更新一条记录的 uuid,就像我创建新记录并且数据库生成 uuid 时一样。

Is it:是吗:

PendingCredit::find(18)->update(['uuid'=>null]);

Or:或者:

PendingCredit::find(18)->update(['uuid'=>DB::raw('UUID()')]);

Or is there a standard way to this?或者有标准的方法吗?

$table->double('amount')->default(1);

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

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