![](/img/trans.png)
[英]Laravel Nova: Add row numbers (NOT ID) to the Laravel Nova resources
[英]Laravel Nova Set ID manually on Form
我想手動設置我的 ID,因為我的 ID 類型是字符串 (varchar)
這是我的模型
<?php
namespace App\Model\Master;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
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 \Illuminate\Support\Collection $fields
* @return \Illuminate\Support\Collection
*/
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 \Illuminate\Support\Collection $fields
* @return \Illuminate\Support\Collection
*/
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.