Laravel Nova在Form上手动设置ID



我想手动设置我的ID由于我的ID类型为字符串(varchar(

这是我的型号

<?php
namespace AppModelMaster;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;
class UnitOfMeasure extends Model 
{
protected $table = 'unit_of_measures';
public $timestamps = true;
public $incrementing = false;
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $fillable = array('id','code', 'description', 'scan_input_required');
public function workCenter()
{
return $this->hasMany(WorkCenter::class,'unit_of_measures_code','code');
}

但Nova总是隐藏ID字段。有办法做到这一点吗?

感谢

如果查看调用creation-fields端点的请求,您会注意到ID甚至不在字段列表中。

资源使用的特征ResolvesFields正在调用一个函数creationFields来生成要在前面显示的字段列表,该函数正在调用removeNonCreationFields

/**
* Remove non-creation fields from the given collection.
*
* @param  IlluminateSupportCollection  $fields
* @return IlluminateSupportCollection
*/
protected function removeNonCreationFields(Collection $fields)
{
return $fields->reject(function ($field) {
return $field instanceof ListableField ||
$field instanceof ResourceToolElement ||
$field->attribute === $this->resource->getKeyName() ||
$field->attribute === 'ComputedField' ||
! $field->showOnCreation;
});
}

由于字段符合以下规则:

$field->attribute === $this->resource->getKeyName()

ID字段将被删除。

要强制字段,您可以在资源中覆盖该函数:

/**
* Remove non-creation fields from the given collection.
*
* @param  IlluminateSupportCollection  $fields
* @return IlluminateSupportCollection
*/
protected function removeNonCreationFields(Collection $fields)
{
return $fields->reject(function ($field) {
return $field instanceof ListableField ||
$field instanceof ResourceToolElement ||
$field->attribute === 'ComputedField' ||
! $field->showOnCreation;
});
}

我自己也遇到了这个问题,看起来Nova已经更新了,所以你可以手动将ID字段添加到资源中:Text::make('ID'),然后你就会有一个可编辑的ID字段。

以下是github问题:https://github.com/laravel/nova-issues/issues/268

最新更新