Skip to content

Commit 3f294c8

Browse files
committed
修改Grid With相关
1 parent 97a5830 commit 3f294c8

File tree

2 files changed

+73
-28
lines changed

2 files changed

+73
-28
lines changed

src/Grid.php

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Closure;
88
use Illuminate\Database\Eloquent\Builder;
99
use Illuminate\Database\Eloquent\Model as Eloquent;
10+
use Illuminate\Database\Eloquent\Relations;
11+
use Illuminate\Support\Str;
1012
use SmallRuralDog\Admin\Components\Component;
1113
use SmallRuralDog\Admin\Grid\Actions;
1214
use SmallRuralDog\Admin\Grid\Column;
@@ -35,8 +37,8 @@ class Grid extends Component implements \JsonSerializable
3537
*/
3638
protected $columns = [];
3739
protected $rows;
38-
public $columnAttributes = [];
39-
protected $withs = [];
40+
protected $columnAttributes = [];
41+
4042

4143
protected $keyName = 'id';
4244
protected $selection = false;
@@ -47,7 +49,7 @@ class Grid extends Component implements \JsonSerializable
4749
private $toolbars;
4850

4951

50-
public function __construct(Eloquent $model, Closure $builder = null)
52+
public function __construct(Eloquent $model)
5153
{
5254
$this->attributes = new Attributes();
5355
$this->dataUrl = request()->getUri();
@@ -76,24 +78,15 @@ public function model()
7678
}
7779

7880

79-
/**
80-
* 获取with
81-
* @return array
82-
*/
83-
public function getWiths(): array
84-
{
85-
return $this->withs;
86-
}
87-
8881
/**
8982
*设置with
9083
* @param array $withs
9184
* @return $this
85+
* @deprecated
9286
*/
9387
public function with(array $withs)
9488
{
95-
$this->withs = $withs;
96-
89+
$this->model()->with($withs);
9790
return $this;
9891
}
9992

@@ -131,6 +124,10 @@ public function tree($tree = true)
131124
*/
132125
public function column($name, $label = '', $columnKey = null)
133126
{
127+
if (Str::contains($name, '.')) {
128+
$this->addRelationColumn($name, $label);
129+
}
130+
134131
return $this->addColumn($name, $label, $columnKey);
135132
}
136133

@@ -148,6 +145,29 @@ protected function addColumn($name = '', $label = '', $columnKey = null)
148145
return $column;
149146
}
150147

148+
/**
149+
* Add a relation column to grid.
150+
*
151+
* @param string $name
152+
* @param string $label
153+
*
154+
* @return $this|bool|Column
155+
*/
156+
protected function addRelationColumn($name, $label = '')
157+
{
158+
list($relation, $column) = explode('.', $name);
159+
160+
$model = $this->model()->eloquent();
161+
162+
163+
if (!method_exists($model, $relation) || !$model->{$relation}() instanceof Relations\Relation) {
164+
} else {
165+
$this->model()->with($relation);
166+
}
167+
168+
169+
}
170+
151171
/**
152172
* @param Column[] $columns
153173
* @deprecated

src/Grid/Model.php

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66

77
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Database\Eloquent\Collection;
89
use Illuminate\Database\Eloquent\Model as EloquentModel;
910
use Illuminate\Database\Eloquent\Relations\Relation;
1011
use Illuminate\Pagination\LengthAwarePaginator;
12+
use Illuminate\Support\Arr;
1113
use Illuminate\Support\Str;
1214
use SmallRuralDog\Admin\Grid;
1315

@@ -24,6 +26,12 @@ class Model
2426
*/
2527
protected $model;
2628

29+
30+
/**
31+
* @var EloquentModel |Builder
32+
*/
33+
protected $sModel;
34+
2735
/**
2836
* @var EloquentModel|Builder
2937
*/
@@ -96,6 +104,7 @@ class Model
96104
public function __construct(EloquentModel $model, Grid $grid = null)
97105
{
98106
$this->model = $model;
107+
$this->sModel = $model;
99108
$this->originalModel = $model;
100109
$this->grid = $grid;
101110
$this->queries = collect();
@@ -172,19 +181,34 @@ protected function setPaginate()
172181
$this->queries->push($query);
173182
}
174183

175-
/**
176-
* 设置预加载
177-
*/
178-
protected function setWith()
184+
185+
public function with($relations)
179186
{
180-
$with = $this->grid->getWiths();
187+
if (is_array($relations)) {
188+
if (Arr::isAssoc($relations)) {
189+
$relations = array_keys($relations);
190+
}
181191

182-
if ($with) $this->queries->push([
183-
'method' => 'with',
184-
'arguments' => $with,
185-
]);
192+
$this->eagerLoads = array_merge($this->eagerLoads, $relations);
193+
}
194+
195+
if (is_string($relations)) {
196+
if (Str::contains($relations, '.')) {
197+
$relations = explode('.', $relations)[0];
198+
}
199+
200+
if (Str::contains($relations, ':')) {
201+
$relations = explode(':', $relations)[0];
202+
}
186203

204+
if (in_array($relations, $this->eagerLoads)) {
205+
return $this;
206+
}
207+
208+
$this->eagerLoads[] = $relations;
209+
}
187210

211+
return $this->__call('with', (array)$relations);
188212
}
189213

190214

@@ -313,22 +337,23 @@ public function get()
313337
if ($this->model instanceof LengthAwarePaginator) {
314338
return $this->model;
315339
}
316-
317340
if ($this->relation) {
318341
$this->model = $this->relation->getQuery();
319342
}
320-
$this->setWith();
343+
321344
$this->setSort();
322345
$this->setPaginate();
323346

324-
//dd($this->queries);
325347

326348
$this->queries->unique()->each(function ($query) {
327349
$this->model = call_user_func_array([$this->model, $query['method']], $query['arguments']);
328350
});
329351

352+
353+
$data = $this->model;
354+
330355
if ($this->model instanceof Collection) {
331-
return $this->model;
356+
return $data;
332357
}
333358

334359
if ($this->model instanceof LengthAwarePaginator) {
@@ -337,7 +362,7 @@ public function get()
337362
'per_page' => $this->model->perPage(),
338363
'last_page' => $this->model->lastPage(),
339364
'total' => $this->model->total(),
340-
'data' => $this->displayData($this->model->getCollection())
365+
'data' => $this->displayData($data->items())
341366
];
342367
}
343368

0 commit comments

Comments
 (0)