gpt4 book ai didi

php - Laravel:创建可空的 morphs() 关系

转载 作者:行者123 更新时间:2023-12-03 15:43:08 34 4
gpt4 key购买 nike

我想创建一个一对一的多态关系允许空关系。

例子:

  Schema::create('ps_assistances', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('assistance_number', 50)->nullable();
$table->morphs('assitanceable')->nullable();
});

但是这个例子在将“->nullable()”赋值给 morph 列时返回 null。

我尝试手动创建 _type 和 _id 并且它工作正常。

手动变形列的示例:

  Schema::create('ps_assistances', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('assistance_number', 50)->nullable();
$table->string('assitanceable_type')->nullable();
$table->unsignedBigInteger('assitanceable_id')->nullable();
});

我想知道是否存在更好的方法来实现一对一的多态关系可空。

最佳答案

nullableMorphs应该为你处理这个

例如:

Schema::create('ps_assistances', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('assistance_number', 50)->nullable();
$table->nullableMorphs('assitanceable');
});

关于php - Laravel:创建可空的 morphs() 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60377003/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com