[英]Limit the number of options returned in the BelongsTo select field for a Laravel Nova resource
[英]Laravel nova save Resource field with belongsTo
我想在Laravel Nova中保存资源。
我实际上有2种资源和2种模型:
客户和客户类型
客户具有以下字段:
Text::make('Vat')
->rules('required', 'string', 'max:254')
->creationRules('unique:customers,vat')
->updateRules('unique:customers,vat,{{resourceId}}'),
BelongsTo::make('Type', 'customer_type', 'App\Nova\CustomerType'),
客户模型具有以下关系customer_type :
public function customer_type(){
return $this->belongsTo('App\Models\CustomerType', 'id', 'type');
}
当我打开create Customer资源视图时 ,可以在选择框中正确看到类型列表,但是当我尝试设置类型并将其保存时,返回SQL错误:
SQLSTATE [HY000]:常规错误:1364字段“类型”没有默认值(SQL:插入
customers
(vat
,id
,name
,updated_at
,created_at
)值(vat,?,test,2019-07-31) 10:41:05,2019-07-31 10:41:05))
如何将类型添加到nova资源保存中?
我还尝试将Relationship的名称从customer_type更改为type ,但是它返回相同的错误。
我的数据库现在是:
客户表
customer_type表:
ID
sl
编辑
我也试过把
BelongsTo::make('CustomerType'),
现场和客户模型:
public function customertype(){
return $this->belongsTo('App\Models\CustomerType', 'id', 'type');
}
但是发生了同样的错误。
对于其他开发人员。
我发现了我的错误。
在我的模型中,我反转了关系的外键和所有者键:
代替:
public function customertype(){
return $this->belongsTo('App\Models\CustomerType', 'id', 'type');
}
应该
public function customertype(){
return $this->belongsTo('App\Models\CustomerType', 'type', 'id');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.