繁体   English   中英

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

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

我有这个迁移:

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');
    }
}

当我为pending_credits创建新记录时,我没有设置uuid属性,因此数据库将生成一个新的 uuid(使用默认值)。

假设出于某种原因,我需要更新一条记录的 uuid,就像我创建新记录并且数据库生成 uuid 时一样。

是吗:

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

或者:

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

或者有标准的方法吗?

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

暂无
暂无

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

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