如何解决通过select2字段类型传递的空值
“我的产品”模型有一个code
外键,它指向一个代码模型,该模型也有一个code
列,但作为主键。在“产品的CRUD的创建”视图中,我创建了一个下拉字段,该字段允许我使用Select2字段类型从所有现有的代码中进行选择。
[
'label' => 'Code','type' => 'select2','name' => 'code',// the foreign key
'entity' => 'productCode',// the relationship's method
'model' => 'App\Models\Code','attribute' => 'code',// attribute that is shown in the dropdown
]
代码在下拉列表中正确显示,但是当我保存新产品时,代码字段作为空值存储在请求中。有人知道这个问题可能是什么吗?还是我弄错了这些?
这是产品模型中的关系:
public function productCode() {
return $this->belongsTo('App\Models\Code','code','code');
}
解决方法
当主键不是典型的“ id”列时,模型上需要primaryKey
属性;如果主键不是整数列,则需要keyType
属性:
将以下内容添加到您的App\Models\Code
模型中可以解决此问题:
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'code';
/**
* The "type" of the primary key
*
* @var string
*/
protected $keyType = 'string';
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。