Skip to content

Commit 6731dd7

Browse files
committed
1 parent addbc6d commit 6731dd7

File tree

6 files changed

+35
-40
lines changed

6 files changed

+35
-40
lines changed

src/Form.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Encore\Admin\Form\Layout\Layout;
1111
use Encore\Admin\Form\Row;
1212
use Encore\Admin\Form\Tab;
13+
use Encore\Admin\Traits\ShouldSnakeAttributes;
1314
use Illuminate\Contracts\Support\Renderable;
1415
use Illuminate\Database\Eloquent\Model;
1516
use Illuminate\Database\Eloquent\Relations;
@@ -74,7 +75,7 @@
7475
*/
7576
class Form implements Renderable
7677
{
77-
use HasHooks;
78+
use HasHooks, ShouldSnakeAttributes;
7879

7980
/**
8081
* Remove flag in `has many` form.
@@ -1121,8 +1122,6 @@ function (Field $field) use ($column) {
11211122
*/
11221123
protected function setFieldOriginalValue()
11231124
{
1124-
// static::doNotSnakeAttributes($this->model);
1125-
11261125
$values = $this->model->toArray();
11271126

11281127
$this->builder->fields()->each(function (Field $field) use ($values) {
@@ -1151,8 +1150,6 @@ protected function setFieldValue($id)
11511150

11521151
$this->callEditing();
11531152

1154-
// static::doNotSnakeAttributes($this->model);
1155-
11561153
$data = $this->model->toArray();
11571154

11581155
$this->builder->fields()->each(function (Field $field) use ($data) {
@@ -1183,20 +1180,6 @@ public function fieldset(string $title, Closure $setCallback)
11831180
return $fieldset;
11841181
}
11851182

1186-
/**
1187-
* Don't snake case attributes.
1188-
*
1189-
* @param Model $model
1190-
*
1191-
* @return void
1192-
*/
1193-
protected static function doNotSnakeAttributes(Model $model)
1194-
{
1195-
$class = get_class($model);
1196-
1197-
$class::$snakeAttributes = false;
1198-
}
1199-
12001183
/**
12011184
* Get validation messages.
12021185
*

src/Form/Field/MultipleSelect.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
66
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Str;
78

89
class MultipleSelect extends Select
910
{
@@ -45,7 +46,7 @@ protected function getOtherKey()
4546
*/
4647
public function fill($data)
4748
{
48-
$relations = Arr::get($data, \Illuminate\Support\Str::snake($this->column));
49+
$relations = Arr::get($data, $this->form->shouldSnakeAttributes() ? Str::snake($this->column) : $this->column);
4950

5051
if (is_string($relations)) {
5152
$this->value = explode(',', $relations);

src/Grid.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Encore\Admin\Grid\Model;
1313
use Encore\Admin\Grid\Row;
1414
use Encore\Admin\Grid\Tools;
15+
use Encore\Admin\Traits\ShouldSnakeAttributes;
1516
use Illuminate\Database\Eloquent\Model as Eloquent;
1617
use Illuminate\Database\Eloquent\Relations;
1718
use Illuminate\Support\Collection;
@@ -33,6 +34,7 @@ class Grid
3334
Concerns\HasSelector,
3435
Concerns\CanHidesColumns,
3536
Concerns\CanFixColumns,
37+
ShouldSnakeAttributes,
3638
Macroable {
3739
__call as macroCall;
3840
}
@@ -385,7 +387,7 @@ protected function addRelationColumn($name, $label = '')
385387
return $this;
386388
}
387389

388-
$name = Str::snake($relation).'.'.$column;
390+
$name = ($this->shouldSnakeAttributes() ? Str::snake($relation) : $relation) .'.'.$column;
389391

390392
$this->model()->with($relation);
391393

@@ -842,7 +844,9 @@ protected function handleRelationColumn($method, $label)
842844
) {
843845
$this->model()->with($method);
844846

845-
return $this->addColumn($method, $label)->setRelation(Str::snake($method));
847+
return $this->addColumn($method, $label)->setRelation(
848+
$this->shouldSnakeAttributes() ? Str::snake($method) : $method
849+
);
846850
}
847851

848852
if ($relation instanceof Relations\HasMany
@@ -852,7 +856,7 @@ protected function handleRelationColumn($method, $label)
852856
) {
853857
$this->model()->with($method);
854858

855-
return $this->addColumn(Str::snake($method), $label);
859+
return $this->addColumn($this->shouldSnakeAttributes() ? Str::snake($method) : $method, $label);
856860
}
857861

858862
return false;

src/Grid/Model.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,6 @@ public function __construct(EloquentModel $model, Grid $grid = null)
113113
$this->grid = $grid;
114114

115115
$this->queries = collect();
116-
117-
// static::doNotSnakeAttributes($this->model);
118-
}
119-
120-
/**
121-
* Don't snake case attributes.
122-
*
123-
* @param EloquentModel $model
124-
*
125-
* @return void
126-
*/
127-
protected static function doNotSnakeAttributes(EloquentModel $model)
128-
{
129-
$class = get_class($model);
130-
131-
$class::$snakeAttributes = false;
132116
}
133117

134118
/**

src/Show.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Encore\Admin\Show\Field;
77
use Encore\Admin\Show\Panel;
88
use Encore\Admin\Show\Relation;
9+
use Encore\Admin\Traits\ShouldSnakeAttributes;
910
use Illuminate\Contracts\Support\Renderable;
1011
use Illuminate\Database\Eloquent\Model;
1112
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -22,6 +23,8 @@
2223

2324
class Show implements Renderable
2425
{
26+
use ShouldSnakeAttributes;
27+
2528
/**
2629
* The Eloquent model to show.
2730
*
@@ -444,7 +447,9 @@ protected function handleRelationField($method, $arguments)
444447
return $this->addRelation($method, $arguments[1], $arguments[0]);
445448
}
446449

447-
return $this->addField($method, Arr::get($arguments, 0))->setRelation(Str::snake($method));
450+
return $this->addField($method, Arr::get($arguments, 0))->setRelation(
451+
$this->shouldSnakeAttributes() ? Str::snake($method) : $method
452+
);
448453
}
449454

450455
if ($relation instanceof HasMany
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Encore\Admin\Traits;
4+
5+
trait ShouldSnakeAttributes
6+
{
7+
/**
8+
* Indicates if model should snake attribute name.
9+
*
10+
* @return bool
11+
*/
12+
public function shouldSnakeAttributes()
13+
{
14+
$class = get_class($this->model);
15+
16+
return $class::$snakeAttributes;
17+
}
18+
}

0 commit comments

Comments
 (0)