Skip to content

Commit e3ca409

Browse files
committed
feat: 判断 column 是否需要蛇形化的属性
1 parent cc3d9d6 commit e3ca409

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/Form/Field.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ class Field implements Renderable
246246
*/
247247
protected $callback;
248248

249+
/**
250+
* column is snake-casing attributes.
251+
*
252+
* @var bool
253+
*/
254+
protected $snakeAttributes = false;
255+
249256
/**
250257
* @var bool
251258
*/
@@ -379,6 +386,42 @@ public function setElementName($name): self
379386
return $this;
380387
}
381388

389+
/**
390+
* Set snake attributes to the field.
391+
*
392+
* @param bool $snakeAttributes
393+
*
394+
* @return $this
395+
*/
396+
public function setSnakeAttributes($snakeAttributes)
397+
{
398+
$this->snakeAttributes = $snakeAttributes;
399+
400+
return $this;
401+
}
402+
403+
/**
404+
* Get snake attributes of the field.
405+
*
406+
* @return bool
407+
*/
408+
public function getSnakeAttributes()
409+
{
410+
return $this->snakeAttributes;
411+
}
412+
413+
/**
414+
* Determine if a column needs to be snaked.
415+
*
416+
* @param string|array $column
417+
*
418+
* @return string|array
419+
*/
420+
protected function columnShouldSnaked($column)
421+
{
422+
return $this->getSnakeAttributes() ? Str::snake($column) : $column;
423+
}
424+
382425
/**
383426
* Fill data to the field.
384427
*
@@ -392,13 +435,13 @@ public function fill($data)
392435

393436
if (is_array($this->column)) {
394437
foreach ($this->column as $key => $column) {
395-
$this->value[$key] = Arr::get($data, Str::snake($column));
438+
$this->value[$key] = Arr::get($data, $this->columnShouldSnaked($column));
396439
}
397440

398441
return;
399442
}
400443

401-
$this->value = Arr::get($data, Str::snake($this->column));
444+
$this->value = Arr::get($data, $this->columnShouldSnaked($this->column));
402445

403446
$this->formatValue();
404447
}

0 commit comments

Comments
 (0)